For 10000 of prime numbers (used to be directly hit the table, now ask how to beg, instant words are poor, or should understand)

Source: Internet
Author: User
Tags square root

For the number of prime numbers within 10000, first consider the problem of this deterministic range, and then consider the complex.

Preface Excerpt: The prime number is the natural numbers that cannot be divisible by other numbers except 1 and itself. Since there is no formula to represent all the primes, the prime number has always been a mystery to mathematicians. Like the famous Goldbach conjecture, the twin prime guess, hundreds of years has not known to attract the world's many excellent mathematicians. Despite their painstaking study and painstaking endeavor, it remains to be seen yet.
since having a computer, people have found 2 by virtue of the power of the computer .216091any number of primes within.
There are many ways to find primes, and the simplest method is to ask for the definition of primes. For a natural number n, with more than 1 of the natural number is less than n to remove the N, if all except, then n is prime, otherwise n is composite.
However, if you use the method of prime number definition to compile a computer program, its efficiency must be very low, because there are many times there are repeated judgments, and some values can be skipped, many of which are worth improving.
First , for a natural number N, as long as it can be divisible by a number other than 1 non-self, it is definitely not a prime, so it is not
must be removed with the other number.
Second, for N, only a prime number less than n can be removed. For example, if n can be divisible by 15, the actual
can be divisible by 3 and 5, if n cannot be divisible by 3 and 5, then n will never be divisible by 15.
Third, for N, it is not necessary to remove all primes from 2 to n 11, simply by removing all prime numbers less than or equal to √n (square root N). This can be proved by contradiction:
if n is composite, then there must be integers greater than 1 and less than n D1 and D2, making the N=d1xd2.
if both D1 and D2 are greater than √n, then there are: N=d1xd2>√nx√n=n.
And This is not possible, so, D1 and D2 must have a less than or equal to √n.
based on the above analysis, the design algorithm is as follows:
(1) using 2,3,5,7 to try to remove the N method to find all the primes within 100. (reduced complexity of n more ... )
(2) The number of primes within 10000 is calculated by the method of all primes within 100.
first, the 2,3,5,7 are stored in a[1], a[2], a[3], a[4], each after each to find a prime, as long as not more than 100, in order to be stored in a cell in a array. It can be found that there are 25 prime numbers within 100; When we ask for a prime number between 100-10000, we can use the prime number of a[1]-a[25] to try to remove N, the prime number in this range can not be saved, direct printing.

Code:

#include <iostream> #include <cstdio> #include <iomanip>using namespace Std;int a[30]; For storing a prime number int j = 5;int line = 0;//for less than 100, the prime number within 100 is first calculated, output and stored. void solve100 () {for (int i = one; I < i++) {if (i% a[2] && i% a[3] && i% a[4] && I% a[5 ] {A[++j] = i;if (line% = = 0) {cout << Endl;} cout << SETW (5) << i;line++;}}} Determine and output 100~10000 prime number void solve10000 () {for (int i = 101; i < 10000; i++) {BOOL flag = true;for (int k = 2; k <= J; +) {if (i% a[k] = = 0) {flag = False;break;}} if (flag) {if (line% = = 0) {cout << Endl;} cout << SETW (5) << i;line++;}}} int main () {//Direct output of prime number within 10 a[0] = 0;a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 5, a[5] = 7;cout << setw (5) << A[2];co UT << SETW (5) << a[3];cout << setw (5) << a[4];cout << setw (5) << a[5];line = 4;solve10 0 (); solve10000 (); cout << "\n10000 number of characters:" << line<< Endl;return 0;}   /* Output 2 3 5 711 13 17 19 23 2931 37 41 43 47 53 59 61 67 7173 79 83 89 97 101 103 107 109 1131 27 131 137 139 149 151 157 163 167 173179 181 191 193 197 199 211 223 227 229233 239 241 251 257 26  3 269 271 277 281283 293 307 311 313 317 331 337 347 349353 359 367 373 379 383 389 397 401 409419 421 431 433 439 443 449 457 461 463467 479 487 491 499 503 509 521 523 541547 557 563 569 571 577 5  87 593 599 601607 613 617 619 631 641 643 647 653 659661 673 677 683 691 701 709 719 727 733739 743  751 757 761 769 773 787 797 809811 821 823 827 829 839 853 857 859 863877 881 883 887 907 911 919 929 937 941947 953 967 971 977 983 991 997 1009 10131019 1021 1031 1033 1039 1049 1051 1061 1063 10691087 1091 10 93 1097 1103 1109 1117 1123 1129 11511153 1163 1171 1181 1187 1193 1201 1213 1217 12231229 1231 1237 1249 1259 1277 1279 1 283 1289 12911297) 1301 1303 1307 1319 1321 1327 1361 1367 13731381 1399 1409 1423 1427 1429 1433 1439 1447 14511453 1459 1471 1481 1483 1487 1489 1493 1499 15111523 1531 1543 1549 1553 1559 1567 1571 1579 15831597 1601 1607 1609 1613 1619 1621 1627 1637 16571663 1667  1669 1693 1697 1699 1709 1721 1723 17331741 1747 1753 1759 1777 1783 1787 1789 1801 18111823 1831 1847 1861 1867 1871 1873  1877 1879 18891901 1907 1913 1931 1933 1949 1951 1973 1979 19871993 1997 1999 2003 2011 2017 2027 2029 2039 20532063 2069 2081 2083 2087 2089 2099 2111 2113 21292131 2137 2141 2143 2153 2161 2179 2203 2207 22132221 2237 2239 2243 2251 2267 226 9 2273 2281 22872293 2297 2309 2311 2333 2339 2341 2347 2351 23572371 2377 2381 2383 2389 2393 2399 2411 2417 24232437 244 1 2447 2459 2467 2473 2477 2503 2521 25312539 2543 2549 2551 2557 2579 2591 2593 2609 26172621 2633 2647 2657 2659 2663 26 71 2677 2683 26872689 2693 2699 2707 2711 2713 2719 2729 2731 27412749 2753 2767 2777 2789 2791 2797 2801 2803 28192833 28 37 2843 2851 2857 2861 2879 2887 2897 29032909 2917 2927 2939 2953 2957 2963 2969 2971 29993001 3011 3019 3023 3037 3041 3049 3061 3067 30793083 3 089 3109 3119 3121 3137 3163 3167 3169 31813187 3191 3203 3209 3217 3221 3229 3251 3253 32573259 3271 3299 3301 3307 3313 3319 3323 3329 33313343 3347 3359 3361 3371 3373 3389 3391 3407 34133433 3449 3457 3461 3463 3467 3469 3491 3499 35113517  3527 3529 3533 3539 3541 3547 3557 3559 35713581 3583 3593 3607 3613 3617 3623 3631 3637 36433659 3671 3673 3677 3691 3697  3701 3709 3719 37273733 3739 3761 3767 3769 3779 3793 3797 3803 38213823 3833 3847 3851 3853 3863 3877 3881 3889 39073911 3917 3919 3923 3929 3931 3943 3947 3967 39894001 4003 4007 4013 4019 4021 4027 4049 4051 40574073 4079 4091 4093 4099 411 1 4127 4129 4133 41394153 4157 4159 4177 4201 4211 4217 4219 4229 42314241 4243 4253 4259 4261 4271 4273 4283 4289 4297432 7 4337 4339 4349 4357 4363 4373 4391 4397 44094421 4423 4441 4447 4451 4457 4463 4481 4483 44934507 4513 4517 4519 4523 45 47 4549 4561) 4567 45834591 4597 4603 4621 4637 4639 4643 4649 4651 46574663 4673 4679 4691 4703 4721 4723 4729 4733 47514759 4783 4787 4789 4793 4 799 4801 4813 4817 48314861 4871 4877 4889 4903 4909 4919 4931 4933 49374943 4951 4957 4967 4969 4973 4987 4993 4999 50035 009 5011 5021 5023 5039 5051 5059 5077 5081 50875099 5101 5107 5113 5119 5147 5153 5167 5171 51795189 5197 5209 5227 5231 5233 5237 5261 5273 52795281 5297 5303 5309 5323 5333 5347 5351 5381 53875393 5399 5407 5413 5417 5419 5431 5437 5441 5443  5449 5471 5477 5479 5483 5501 5503 5507 5519 55215527 5531 5557 5563 5569 5573 5581 5591 5623 56395641 5647 5651 5653 5657 5659 5669 5683 5689 56935701 5711 5717 5737 5741 5743 5749 5779 5783 57915801 5807 5813 5821 5827 5839 5843 5849 5851 585 75861 5867 5869 5879 5881 5897 5903 5923 5927 59395953 5981 5987 6007 6011 6029 6037 6043 6047 60536067 6073 6079 6089 609 1 6101 6113 6121 6131 61336143 6151 6163 6173 6197 6199 6203 6211 6217 62216229 6247 6257 6263 6269 6271 6277 6287 6299 63 016311 6317 6323) 6329 6337 6343 6353 6359 6361 63676373 6379 6389 6397 6421 6427 6449 6451 6469 64736481 6491 6521 6529 6547 6551 6553 6563 6569 6 5716577 6581 6599 6607 6619 6637 6653 6659 6661 66736679 6689 6691 6701 6703 6709 6719 6733 6737 67616763 6779 6781 6791 6 793 6803 6823 6827 6829 68336841 6857 6863 6869 6871 6883 6899 6907 6911 69176947 6949 6959 6961 6967 6971 6977 6983 6991 69977001 7013 7019 7027 7039 7043 7057 7069 7079 71037109 7121 7127 7129 7151 7159 7177 7187 7193 72077211 7213 7219 7229  7237 7243 7247 7253 7283 72977307 7309 7321 7331 7333 7349 7351 7369 7393 74117417 7433 7451 7457 7459 7477 7481 7487 7489  74997507 7517 7523 7529 7537 7541 7547 7549 7559 75617573 7577 7583 7589 7591 7603 7607 7621 7639 76437649 7669 7673 7681 7687 7691 7699 7703 7717 77237727 7741 7753 7757 7759 7789 7793 7817 7823 78297841 7853 7867 7873 7877 7879 7883 7901 790 7 79197927 7933 7937 7949 7951 7963 7993 8009 8011 80178039 8053 8059 8069 8081 8087 8089 8093 8101 81118117 8123 8147 816 1 8167 8171 8179 8191 8209 82198221 8231 8233 8237 8243 8263 8269 8273 8287 82918293 8297 8311 8317 8329 8353 8363 8369 8377 83878389 8419 8423 84 29 8431 8443 8447 8461 8467 85018513 8521 8527 8537 8539 8543 8563 8573 8581 85978599 8609 8623 8627 8629 8641 8647 8663 8 669 86778681 8689 8693 8699 8707 8713 8719 8731 8737 87418747 8753 8761 8779 8783 8803 8807 8819 8821 88318837 8839 8849 8 861 8863 8867 8887 8893 8923 89298933 8941 8951 8963 8969 8971 8999 9001 9007 90119013 9029 9041 9043 9049 9059 9067 9091 9103 91099127 9133 9137 9151 9157 9161 9173 9181 9187 91999203 9209 9221 9227 9239 9241 9257 9277 9281 92839293 9311 9319  9323 9337 9341 9343 9349 9371 93779391 9397 9403 9413 9419 9421 9431 9433 9437 94399461 9463 9467 9473 9479 9491 9497 9511  9521 95339539 9547 9551 9587 9601 9613 9619 9623 9629 96319643 9649 9661 9677 9679 9689 9697 9719 9721 97339739 9743 9749 9767 9769 9781 9787 9791 9803 98119817 9829 9833 9839 9851 9857 9859 9871 9883 98879901 9907 9923 9929 9931 9941 9949 996 7 997310000 The number of characters is: 1229Please press any key to continue ... */ 

The calculated result is the same as the 1229 prime numbers calculated by the prime number theorem and can be verified by itself.

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

A solution to the prime number within 10000 is briefly described, and the following is a two mathematical approach:

The number of primes is calculated by sieve method.
A brief introduction to the Eratosthenes sieve method. Ernie Eratosthenes, an ancient Greek mathematician, used a different approach when looking for prime numbers: first write the numbers of 2-n on paper:
draw a circle on the top of 2, and then draw the other multiples of 2, the first number that is neither circular nor drawn is 3, it is circled, and then the other multiples of 3 are drawn; The first number that is neither circular nor crossed is 5, it is circled, and the other multiples of 5 are drawn ... And so on, until all the numbers less than or equal to n are circled or drawn. At this point, the number of circles in the table and those that are not crossed are just the primes less than N.
It is like a sieve that leaves the number of satisfied conditions and filters out the number of unsatisfied conditions. Since this method was first invented by Ernie Eratosthenes, this method is called the Eratosthenes sieve by posterity.
in the computer, the Sieve method can be used to zero the array element to achieve. Specifically: first open an array: a[i],i=1,2,3, ..., at the same time, make all array elements equal to the subscript value, that is, a[i]=i, when I is not a prime number, make a[i]=0. When the output results, as long as the judge A[i] is equal to zero, if a[i]=0, then i=i+1, check the next a[i].
Sieve method is one of the commonly used algorithms in computer programming.

Take a picture to illustrate:



Code:

#include <iostream> #include <cstdio> #include <iomanip>using namespace Std;int a[10005];void init () { for (int i = 0; I <= 10000; i++) {a[i] = i;}} void Solve () {for (int i = 2; I <= 10000; i++) {while (a[i] = = 0) {i++;} for (int k = 2, j = k * I; J <= 10000; j = (++k) * i) {a[j] = 0;}}} void Printans () {int line = 0;for (int i = 2, i < 10000; i++) {while (a[i] = = 0) {i++;} if (I >= 10000) {break;} if (line! = 0 && Line% = = 0) {cout << Endl;} cout << SETW (5) << i;line++;} cout << "Number of \n10000 Within": "<< line << Endl;} int main () {init (); solve ();p Rintans (); return 0;}
Go to the first result of the same.


The prime number is calculated by 6n±1 method.
any natural number can always be expressed as one of the following forms:
6n,6n+1,6n+2,6n+3,6n+4,6n+5 (n=0,1,2, ...)
obviously, when n≥1, 6n,6n+2,6n+3,6n+4 is not a prime number, only the natural numbers such as 6n+1 and 6n+5 may be prime. Therefore, all primes, except 2 and 3, can be represented as 6n±1 (n is the natural number).
according to the above analysis, we can construct the other side of the sieve, only the shape as 6 n±1 natural number screening, so that can greatly reduce the number of screening, so as to further improve the operation efficiency and speed of the program.
in the program, we can use a double loop to achieve this, the outer loop i in multiples of 3 increments, the inner loop j is 0-1 of the cycle, then 2 (i+j) 1 happens to be shaped like 6n±1 natural number.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

For 10000 of prime numbers (used to be directly hit the table, now ask how to beg, instant words are poor, or should understand)

Related Article

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.