Extensible prime generator: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (Fix '<<syntaxhighlight>' throughout entire page) |
Antoni Gual (talk | contribs) |
||
Line 5,818: | Line 5,818: | ||
The last prime I could find is the : 7 550 283th, Value : 133 218 289</pre> |
The last prime I could find is the : 7 550 283th, Value : 133 218 289</pre> |
||
=={{header|VBScript}}== |
|||
<syntaxhighlight lang="vb"> |
|||
'uses variant booleans for the sieve, so 16 bytes per bit, a little wasteful! |
|||
Option Explicit |
|||
Sub sieveit(maxi) 'increases sieve size up to maxi and sieves the added part |
|||
Dim lasttop,a,b,c,i,j |
|||
lasttop=UBound(primes) |
|||
If maxi>lasttop Then |
|||
ReDim preserve primes(maxi) |
|||
print vbcrlf &"(sieving from " & lasttop & " up to " & maxi &")"& vbCrLf |
|||
For i=lasttop+1 To maxi |
|||
primes(i)=True |
|||
next |
|||
For i=2 To Int(Sqr(lasttop)) |
|||
If primes(i)=True Then |
|||
a=lasttop\i |
|||
b=maxi\i |
|||
c= i*a |
|||
For j=a To b |
|||
primes(c)=False |
|||
c=c+i |
|||
Next |
|||
End if |
|||
Next |
|||
For i=Int(Sqr(lasttop)) To Int(Sqr(maxi)) |
|||
If primes(i)=True Then |
|||
c=i*i |
|||
While c<=maxi |
|||
primes(c)=False |
|||
c=c+i |
|||
wend |
|||
End if |
|||
next |
|||
End if |
|||
End Sub |
|||
function nth(n) 'returns the nth prime (sieves if needed) |
|||
Dim cnt,i,m |
|||
m=Int(n *(Log(n)+Log(Log(n)))) |
|||
If m>UBound(primes) Then sieveit (m) |
|||
i=1 |
|||
Do |
|||
i=i+1 |
|||
If primes(i) Then cnt=cnt+1 |
|||
Loop until cnt=n |
|||
nth=i |
|||
End function |
|||
Sub printprimes (x1, x2,p) ' counts and prints (if p=true) primes between x1 and x2 (sieves if needed) |
|||
Dim lasttop,n,cnt,i |
|||
If x2> UBound(primes) Then sieveit(x2) |
|||
print "primes in range " & x1 & " To " & x2 & vbCrLf |
|||
cnt=0 |
|||
For i=x1 To x2 |
|||
If primes(i) Then |
|||
If p Then print i & vbTab |
|||
cnt=cnt+1 |
|||
End if |
|||
next |
|||
print vbCrLf & "Count: " & cnt |
|||
End Sub |
|||
Sub print(s): |
|||
On Error Resume Next |
|||
WScript.stdout.WriteLine (s) |
|||
If err= &h80070006& Then WScript.Echo " Please run this script with CScript": WScript.quit |
|||
End Sub |
|||
' main program------------------------------------------- |
|||
Dim n |
|||
' initialization of the array of booleans |
|||
reDim Primes(2) |
|||
primes(0) = False |
|||
Primes(1) = False |
|||
Primes(2) = True |
|||
'Show the first twenty primes. |
|||
n=nth(20) |
|||
printprimes 1,n,1 |
|||
'Show the primes between 100 and 150. |
|||
printprimes 100,150,1 |
|||
'Show the number of primes between 7,700 and 8,000. |
|||
printprimes 7700,8000,0 |
|||
'Show the 10,000th prime. |
|||
n= nth(10000) |
|||
print n & " is the " & 10000 & "th prime" |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<small> |
|||
<pre> |
|||
(sieving from 2 up to 81) |
|||
primes in range 1 To 71 |
|||
2 |
|||
3 |
|||
5 |
|||
7 |
|||
11 |
|||
13 |
|||
17 |
|||
19 |
|||
23 |
|||
29 |
|||
31 |
|||
37 |
|||
41 |
|||
43 |
|||
47 |
|||
53 |
|||
59 |
|||
61 |
|||
67 |
|||
71 |
|||
Count: 20 |
|||
(sieving from 81 up to 150) |
|||
primes in range 100 To 150 |
|||
101 |
|||
103 |
|||
107 |
|||
109 |
|||
113 |
|||
127 |
|||
131 |
|||
137 |
|||
139 |
|||
149 |
|||
Count: 10 |
|||
(sieving from 150 up to 8000) |
|||
primes in range 7700 To 8000 |
|||
Count: 30 |
|||
(sieving from 8000 up to 114306) |
|||
104729 is the 10000th prime |
|||
</pre> |
|||
</small> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
This uses a larger wheel than the sieve in the Wren-math module and is a bit faster if one is sieving beyond about 10^7. |
This uses a larger wheel than the sieve in the Wren-math module and is a bit faster if one is sieving beyond about 10^7. |