Analysis of integer prime factor decomposition and sieve Prime Number

Source: Internet
Author: User
# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.