Using C language to realize prime sieve method to obtain all primes within 100 million (100000000)

Source: Internet
Author: User

The specific Sieve method is: first, the N natural numbers in order. 1 is not a prime, nor a composite, to be crossed. The second number 2 is the prime number left, and all the numbers after 2 that can be divisible by 2 are crossed. 2 The number after the first not to be crossed is 3, leave 3, and then divide 3 all the numbers divisible by 3. 3 The number after the first not to be crossed is 5, leave 5, and then divide 5 all the numbers divisible by 5. This has been done, will be not more than N of all composite are screened out, leaving is not more than N of all prime numbers. Because the Greeks are to write the number on the painted wax plate, each to draw a number, on the top of the small point, the search for prime numbers after the completion of the work, many small dots like a sieve, so the Eratosthenes method is called "Eratosthenes sieve Method", referred to as "Sieve method".

Here's the code:

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5 intMainintargcChar*argv[])6 {
Looking for all primes between 2~num7 if(ARGC <2)8 {9printf"Usage:%s num\n", argv[0]);Ten return 0; One } A intIMax = Atoi (argv[1]); - - if(IMax <2) the { -printf"num is too little, num >=2"); - return 0; - } + - Char*p = (Char*) malloc (sizeof(Char) * IMax +1); +Memset (P,sizeof(Char) * IMax +1,0); A at inti =0, j =0, k =0; - for(i =2; I <= IMax; i++) - { - for(j = i + i; j <= IMax; J + =i) - { -P[J] =1; in } - } toFILE * fp =NULL;
After the execution of the program, the number of prime numbers we need is in the file Prime-number.txt + if(fp = fopen ("Prime-number.txt","W")) ==NULL) - { the return 0; * } $K =0;Panax Notoginseng intIall =0; - for(i =2; I <= IMax; i++) the { + if(0==P[i]) A { theIall + +; +k++; - //output to FILE:FP, write these primes to the file $fprintf (FP,"%6d", i); $ if(Ten==k) - { -fprintf (FP,"\ n"); theK =0; - }Wuyi //printf ("%d", I); the } - } Wuprintf"\ n"); - fclose (FP); About Free (p); $printf"All :%d\n", Iall); - - return 0; -}

The output is put in Baidu net disk: Http://pan.baidu.com/s/1pJv58Wb

Storm

Mail: [Email protected]

Using C language to realize prime sieve method to obtain all primes within 100 million (100000000)

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.