Originally Posted by
_jamie
this is what I've came up so far, next step for me is work on adding the code for the largest prime factor instead of all 4 of them being printed out. Thanks to all on the forum for the continuous help. FYI the numbers in the new code i am posting is a 12 digit long number which is part of the actual question instead of a 5 digit int on my first post.
Problem 3 - Project Euler
Code:
/* What is the largest prime factor of the number 600851475143 ? */
// 6857
#include <stdio.h>
int main (void)
{
long num = 600851475143;
long a = 2;
// while (a < num)
while (num != 1)
{
if (num % a == 0)
{
while (num % a == 0)
{
num = num / a;
printf("%ld, ", a);
}
}
a++;
}
printf("\n");
return 0;
}
Well, you've already done it. Remove line 22 (the printf in the loop) and change line 30 to simply print the value of a
Edit:
Code:
$ factor 600851475143
600851475143: 71 839 1471 6857
You're eliminating factors in ascending order so the value of 'a' when the while loop finishes is the largest factor. If you want to be paranoid I guess you could write an isPrime function and check that 6857 is truly prime (but it must be because you've already eliminate any prime or composite factors less than 6857, so 6857 must be prime) (and the command line factor program prints prime factors only, so if you're getting 6857 then...)