uva 10392 - Factoring Large Numbers

好水。。以至于我循环的时候都忘了开方。。。

#include <stdio.h> #include <string.h> #include <math.h> bool isPrime[1000001]; long long prime[78500]; long long pn = 0; void init() { memset(isPrime, true, sizeof(isPrime)); for(int i = 2; i * i <= 1000000; i++) { if(isPrime[i]) { for(int j = i * i; j <= 1000000; j += i) isPrime[j] = false; } } for(int i = 2; i <= 1000000; i++) if(isPrime[i]) prime[pn++] = i; } int main() { init(); long long n; while(scanf("%lld", &n) != EOF && n >= 0 ) { for(long long i = 0; i < pn; i++) { if(n == 1) break; while(n % prime[i] == 0) { printf(" %lld/n", prime[i]); n /= prime[i]; } } long long m = sqrt(n + 0.5); for(long long i = prime[pn - 1] + 1; i <= m; i++) { if(n == 1) break; while(n % i == 0) { printf(" %lld/n", i); n /= i; } } if(n != 1) printf(" %lld/n", n); printf("/n"); } return 0; } 

你可能感兴趣的:(Numbers)