here is my code in C for problem#3 from project-Euler, where I have to find the largest prime factor of 600851475143.
#include
#include
I suggest you to improve the primality check part of your code. The running time of your method is O(n2) so you should use a more efficient algorithm for this like the well-known Miller–Rabin primality test with O(klog3n). I provide a pseudo code here for you and you can write the code on your own:
Input: n > 3, an odd integer to be tested for primality;
Input: k, a parameter that determines the accuracy of the test
Output: composite if n is composite, otherwise probably prime
write n − 1 as 2s·d with d odd by factoring powers of 2 from n − 1
WitnessLoop: repeat k times:
pick a random integer a in the range [2, n − 2]
x ← ad mod n
if x = 1 or x = n − 1 then do next WitnessLoop
repeat s − 1 times:
x ← x2 mod n
if x = 1 then return composite
if x = n − 1 then do next WitnessLoop
return composite
return probably prime
I provide a link for you to see an implementation in python that also compares this algorithm with yours. BTW, there are many implementations of this algorithm all over the web but I think righting it by yourself may help you to better understand it.