# Include <iostream> # include <cstdio> # include <string. h> # include <cmath> # include <time. h> using namespace STD; # define n 1000000 // 12031230int digit [N]; // records the prime factor int num [N]; // records the number of each prime factor int C; // record the number of prime factors void divide (int n) {memset (digit, 0, sizeof (DIGIT); memset (Num, 0, sizeof (Num )); int temp = N; For (long I = 2; I <= N; I ++) {If (TEMP % I = 0) {digit [c] = I; num [c] = 0; C ++;} while (TEMP % I = 0 ){ Num [C-1] ++; temp/= I ;}} typedef long ll; LL Prime [N], nprime, factor [N], numfactor [N], CT; bool isprime [N]; void makeprime () {// 1 ~ Prime Number table of N int I, j, temp; nprime = 0; memset (isprime, 1, sizeof (isprime); isprime [1] = 0; temp = SQRT (n + 0.0); for (I = 2; I <= temp; ++ I) {If (isprime [I]) {++ nprime; prime [nprime] = I; for (j = I + I; j <n; j + = I) {isprime [J] = 0 ;}}} void makeprime2 () {// linear screening prime memset (isprime, 1, sizeof (isprime); int I, j; int num = 0; for (I = 2; I <= N; I ++) {If (isprime [I]) prime [num ++] = I; for (j = 1; j <num & I * prime [J] <= N; j ++) {isprime [I * prime [J] = 0; if (I % prime [J] = 0) Break ;}} void divide2 (ll n) {// perform prime number decomposition int I; int temp = SQRT (n + 0.0); Ct = 0; memset (numfactor, 0, sizeof (numfactor); for (I = 1; I <= nprime; ++ I) {If (prime [I]> temp) break; If (N % prime [I] = 0) {factor [++ CT] = prime [I]; while (N % prime [I] = 0) {numfactor [cT] ++; n/= prime [I] ;}} if (n! = 1) {factor [++ CT] = N; numfactor [cT] ++ ;}} int main () {// for a single number clock_t T1 = clock (), t2; divide (n); t2 = clock (); printf ("cost time is: % d \ n", t2-t1); T1 = clock (); makeprime (); divide2 (n); t2 = clock (); printf ("cost time is: % d \ n", t2-t1); T1 = clock (); makeprime2 (); divide2 (n); t2 = clock (); printf ("cost time is: % d \ n", t2-t1); // complexity is also O (N ), because SQRT T1 = clock (); makeprime (); t2 = clock (); printf ("cost time is: % d \ n", t2-t1 ); // so-called linear sieve prime number T1 = clock (); makeprime2 (); t2 = clock (); printf ("cost time is: % d \ n", t2-t1 ); return 0 ;}
Running result:
Analysis of integer prime factor decomposition and sieve Prime Number