# Project Euler problem 3

• 08-27-2009
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?
• 08-27-2009
Dino
Quote:

The machine is just not running this code
Not very descriptive.
• 08-27-2009
BEN10
Quote:

Originally Posted by SELFMADE
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.........................
• 08-27-2009
MK27
Quote:

Originally Posted by SELFMADE
What is the largest prime factor of the number 600851475143 ?

You need a long int ("%li").
• 08-28-2009
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.
• 08-28-2009
BEN10
Quote:

Originally Posted by SELFMADE
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.
• 09-16-2009
EulerBeuler
Quote:

Originally Posted by SELFMADE
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.
• 09-16-2009
anon
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.