小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

nth prime number contain n as their substring within 100000000

A small VBA code to get these numbers:

 

  1. Sub Main()
  2.     Dim i&, j&, k&, n&, num&, a(100000000) As Byte, p(1 To 10000000) As Long
  3.         num = 1
  4.      p(num) = 2    'The 1st prime
  5.     n = 100000000    '10^8
  6.     k = Int(Sqr(n)) 'sqrare root of n
  7.     For i = 3 To k Step 2
  8.         If a(i) = 0 Then
  9.             num = num + 1
  10.             p(num) = i
  11.             If InStr(i, num) Then Debug.Print "P(" & num & ")=" & i
  12.             For j = i * i To n Step 2 * i    'Eractosthenes
  13.                 a(j) = 100    'Not prime number
  14.             Next
  15.         End If
  16.     Next
  17.     For i = k + 1 To n Step 2    'List all prime numbers to array p()
  18.         If a(i) = 0 Then
  19.             num = num + 1
  20.             'p(num) = i
  21.             If InStr(i, num) Then Debug.Print "P(" & num & ")=" & i
  22.         End If
  23.     Next
  24. End Sub

 

It returns:

 

P(7)=17
P(6455)=64553
P(6456)=64567
P(6457)=64577
P(6459)=64591
P(6460)=64601
P(6466)=64661
P(9551)=99551
P(303027)=4303027
P(440999)=6440999
P(968819)=14968819
P(5517973)=95517973

posted on 2009-01-11 21:27  王峰炬  阅读(152)  评论(0编辑  收藏  举报

导航