Problem
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
- #include <stdio.h>
-
-
#define NUM 600851475143UL
-
/* #define NUM 13195 */
-
-
int isprime(int n)
-
{
-
int i;
-
-
for (i=3; i < n; i+=2) {
-
if (!(n%i))
-
return 0;
-
}
-
-
return 1;
-
}
-
-
-
int main(int argc, const char *argv[])
-
{
-
unsigned long long i, prime, num;
-
-
num = NUM;
-
for (i=3; i <= num; i+=2) {
-
if (!(NUM % i)) {
-
printf("%llu\n", i);
-
if (isprime(i)) {
-
prime = i;
-
}
-
num /= i;
-
printf("num: %llu\n", num);
-
}
-
}
-
-
printf("Largest prime: %llu\n", prime);
-
-
return 0;
-
}
is_prime 还可再优化。在下一道题中呈现。
阅读(1003) | 评论(0) | 转发(0) |