The approximate formula for the nth Prime Number

Source: Internet
Author: User
Introduction

According to the famous Prime Number Theorem:

The approximate formula of the nth prime number can be deduced accordingly, as shown below:

The previous formula is on Wikipedia, and the last one is on "Specific mathematics: computer science basics (English Version 2nd)", which appears in exercise 593rd on page 1. These two formulas are essentially the same.

Test Program

Let's write a C # program to calculate the relative error of this formula:

 1   Using  System;  2   3   Static   Class  Primenth  4  {  5     Static   Decimal [] Primes = 6   {  7       2 , 29 , 541 , 7919 , 104729 , 1299709 , 15485863 , 179424673 ,2038074743  ,  8       22801763489 , 252097800623 , 2760727302517 , 29996224275833  ,  9       323780508946331 , 3475385758524527 , 37124508045065437  ,  10       394906913903735329 , 4185296581467695669 M, 44211790234832169331 m,  11   465675465116607065549 M, 4892055594575155744537 m  12   };  13    14     Static   Void  Main ()  15   {  16 Console. writeline ( "  -M -------------- (10 ^ m)-th-prime-rel-Error "  );  17       For ( VaR M = 1 ; M <primes. length; m ++ ) Run (m );  18   }  19      20     Static   Void Run ( Int  M)  21  {  22       VaR P = Primes [m];  23       VaR R = math. Abs (Pn (( Decimal ) Math. Pow ( 10 , M)-P )/ P;  24 Console. writeline ( "  {} {: N0} {: P6}  "  , M, P, R );  25  }  26      27     Static   Decimal Pn ( Decimal  N)  28   {  29       VaR LnN = math. Log (( Double  ) N );  30       VaR Lnlnn = Math. Log (lnn ); 31       VaR PNN = lnn + lnlnn- 1 + (Lnlnn- 2 )/ Lnn  32 -(Lnlnn * lnlnn- 6 * Lnlnn + 11 )/ 2 /Lnn/ Lnn;  33       Return N *( Decimal  ) PNN; 34   }  35 }

This program stores 5th rows to 12th rows of arrays.0, 101,..., 1020Prime Number. data comes from reference [1].

Compile and run

Compile and run Windows 7. NET Framework 4.5:

D: \ work> CSC primenth. CS Microsoft (r) Visual C # compiler version 4.0.30319.17929 is used for Microsoft (R). Net Framework 4.5 copyright ownership (c) Microsoft Corporation. All rights reserved. D: \ work> Primenth -M -------------- (10 ^ m) -Th-prime-rel-Error 1 29 65.544667% 2 541 8.846891% 3 7,919 1.531065% 4 104,729 5 0.321614% 1,299,709 6 0.083770% 15,485,863 7 0.031450% 8 179,424,673 0.000099% 9 2,038,074,743 0.000190% 10 22,801,763,489 11 0.000846% 0.000464% 12 29,996,224,275,833 0.000312% 13 323,780,508,946,331 0.000211% 14 3,475,385,758,524,527 15 0.000145% 37,124,508,045,065,437 16 0.000101% 17 394,906,913,903,735,329 0.000071% 18 4,185,296,581,467,695,669 0.000051% 19 44,211,790,234,832,169,331 0.000037% 20 465,675,465,116,607,065,549 0.000027%
References
    1. oeis: a006988
    2. the prime Database: the nth prime page
    3. Wikipedia: Prime Number Theorem
    4. Wolfram mathworld: Prime formulas
    5. concrete mathematics: a foundation for computer science, Second Edition

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.