Output all prime numbers in a specified range

Source: Internet
Author: User

# Include <cmath> <br/> # include <fstream> <br/> # include <iomanip> <br/> # include <iostream> <br/> using namespace STD; </P> <p> void printprimeupto (STD: ostream & OS, int upper); <br/> int getdigitnum (INT upper ); </P> <p> int main (void) {<br/> ofstream OUTFILE ("prime.txt"); <br/> If (! OUTFILE) {<br/> cerr <"can not open output file" <Endl; <br/> return-1; <br/>}< br/> printprimeupto (OUTFILE, 999); <br/> return 0; <br/>}</P> <p> int getdigitnum (INT upper) {<br/> upper = ABS (upper); <br/> int num = 0; <br/> do {<br/> + + num; <br/> upper/= 10; <br/>}while (upper); <br/> return num; <br/>}</P> <p> void printprimeupto (STD: ostream & OS, int upper) {<br/> If (upper <2) <br/> RET Urn; <br/> int size = upper-1; <br/> bool * t = new bool [size]; <br/> for (INT I = 0; I! = Size; ++ I) <br/> T [I] = true; <br/> int Index = 0, P; <br/> while (true) {<br/> while (! T [Index]) + + index; <br/> P = index + 2; <br/> If (p * P> upper) <br/> break; </P> <p> Int J = 2, mul; <br/> while (true) {<br/> Mul = p * J; <br/> If (Mul> upper) <br/> break; <br/> T [mul-2] = false; <br/> ++ J; <br/>}< br/> + + index; <br/>}< br/> int CNT = 0, blank = getdigitnum (upper) + 1; <br/> for (INT I = 0; I! = Size; ++ I) {<br/> If (T [I]) {<br/> ++ CNT; <br/> OS <SETW (blank) <I + 2; <br/> If (CNT % 10 = 0) <br/> OS <STD: Endl; <br/>}< br/> OS <STD: Endl; <br/> Delete [] T; <br/>}
 

The above program outputs all quality numbers not greater than 999 to generate formatted output. The program uses the screening idea to exclude all positive integers within a specified range, and all the surviving integers are prime numbers. For more information about this algorithm, see Wikipedia.Sieve of Eratosthenes
Related entries. Note that only SQRT (upper) is used for the outer loop in the above C ++ implementation. The proof is as follows:

Assume that there is a non-prime number n that satisfies SQRT (upper) <n <= upper and it is not excluded by the program,

Then N can be written in the form of N = K * t, where K and T are both positive integers, and at least one number of K and T is smaller than SQRT (upper ), otherwise, there will be a conflict between N> upper.

If the number is T, T has been checked by the program in the previous loop. There are two possible cases:

1. t is not an integer multiple of any positive integers smaller than T

In the inner layer loop, we apply the following parameters to all T * 2, T * 3, T * 4 ,......, T * k ,...... After the check, the number T * k is inevitably excluded and the contradiction is fixed.

2. There is a smallest positive integer d smaller than T, and T is the integer multiple of D.

T is checked in the outer loop before the program, and D is excluded as a multiple of T. For K * t, it must be an integer multiple of D, which will also be excluded and contradictory.

Therefore, in the outer loop, the program only needs to check all the numbers not greater than SQRT (upper), saving a considerable amount of time.

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.