# Thread: Project Euler problem 3

1. ## Project Euler problem 3

What is the largest prime factor of the number 600851475143 ?

My attempt:
Code:
```#include <stdafx.h>

int main()
{
int num1, num2, num3, num4, lrgprime, sum;
lrgprime=num2=num4=sum=0;
for (num1=2; num1<300425737572; num1++) {
num2=600851475143%num1;

if (num2==0) {
for (num3=2; num3<=num1; num3++) {
num4=num1%num3;
if (num4==0 && num1==num3 && num1>=lrgprime)
lrgprime=num1;
}
}
}
printf("This is the largest prime factor: %d", lrgprime);
return 0;
}```
The machine is just not running this code

What did I do wrong?

2. The machine is just not running this code
Not very descriptive.

What is the largest prime factor of the number 600851475143 ?

My attempt:
Code:
```#include <stdafx.h>

int main()
{
int num1, num2, num3, num4, lrgprime, sum;
lrgprime=num2=num4=sum=0;	for (num1=2; num1<300425737572; num1++) {
num2=600851475143%num1;

if (num2==0) {
for (num3=2; num3<=num1; num3++) {
num4=num1%num3;
if (num4==0 && num1==num3 && num1>=lrgprime)
lrgprime=num1;
}
}
}
printf("This is the largest prime factor: %d", lrgprime);
return 0;
}```
The machine is just not running this code

What did I do wrong?
What's that? int, float char, double, long double.........................

What is the largest prime factor of the number 600851475143 ?
You need a long int ("%li").

5. I've been trying to solve this problem for the last 3 days without success can someone help me?

All I need is a function that checks whether a parameter is a prime or not.

I've been trying to solve this problem for the last 3 days without success can someone help me?

All I need is a function that checks whether a parameter is a prime or not.
A prime number is one that is divisible by 1 and itself only. So, you just run a loop starting from 2 till the number and check if it leaves any remainder while dividing with numbers in between. If it leaves a remainder, you need to continue, if the remainder is zero it's not a prime number.

What is the largest prime factor of the number 600851475143 ?

My attempt:
Code:
```#include <stdafx.h>

int main()
{
int num1, num2, num3, num4, lrgprime, sum;
lrgprime=num2=num4=sum=0;
for (num1=2; num1<300425737572; num1++) {
num2=600851475143%num1;

if (num2==0) {
for (num3=2; num3<=num1; num3++) {
num4=num1%num3;
if (num4==0 && num1==num3 && num1>=lrgprime)
lrgprime=num1;
}
}
}
printf("This is the largest prime factor: %d", lrgprime);
return 0;
}```
The machine is just not running this code

What did I do wrong?
Hello,
first of all, integers are usually 32 bit long, i.e. you are limited to numbers between 0 an ~4.000.000.000 for unsigned integers.
For Euler problem #3 you need integers of 64 bit length.
Try to declare these numbers using one of these types:

long long n = 600851475143LL, i, prime;
or
int64_t n = 600851475143LL, i, prime;
or
__int64 n = 600851475143LL, i, prime;

Important is to add 'LL' at the end of the large numbers to tell the compiler to interprete this as an integer of 64 bit. (This is what I just learned and helped me to solve Euler #3).

Hope that helps.
A.

8. I suggest you start with smaller values. Your program says the largest prime factor of 200 is 50.

It also looks like the complexity of the algorithm is too bad - you find all factors, and then you attempt to run a primality test on each of those? Just so you know, there must be better ways.

However I don't think this is where better solutions should be discussed. The point of a challenge is that you do it yourself.