10001th prime: Difference between revisions
Content added Content deleted
(→OCaml: add) |
(Dialect of BASIC moved to the BASIC section.) |
||
Line 126: | Line 126: | ||
print prime(10001) |
print prime(10001) |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
==={{header|FreeBASIC}}=== |
|||
<syntaxhighlight lang="freebasic"> |
|||
#include "isprime.bas" |
|||
function prime( n as uinteger ) as ulongint |
|||
if n=1 then return 2 |
|||
dim as integer p=3, pn=1 |
|||
while pn<n |
|||
if isprime(p) then pn + = 1 |
|||
p += 2 |
|||
wend |
|||
return p-2 |
|||
end function |
|||
print prime(10001) |
|||
</syntaxhighlight> |
|||
{{out}}<pre>104743</pre> |
|||
==={{header|GW-BASIC}}=== |
|||
{{works with|BASICA}} |
|||
<syntaxhighlight lang="gwbasic">10 PN = 1 |
|||
20 P = 3 |
|||
30 WHILE PN < 10001 |
|||
40 GOSUB 100 |
|||
50 IF Q = 1 THEN PN = PN + 1 |
|||
60 P = P + 2 |
|||
70 WEND |
|||
80 PRINT P - 2 |
|||
90 END |
|||
100 REM Tests if a number is prime |
|||
110 Q = 0 |
|||
120 IF P = 2 THEN Q = 1: RETURN |
|||
130 IF P = 3 THEN Q = 1: RETURN |
|||
140 I = 1 |
|||
150 I = I + 2 |
|||
160 IF INT(P / I) * I = P THEN RETURN |
|||
170 IF I * I <= P THEN GOTO 150 |
|||
180 Q = 1 |
|||
190 RETURN</syntaxhighlight> |
|||
{{out}}<pre>104743</pre> |
|||
==={{header|PureBasic}}=== |
==={{header|PureBasic}}=== |
||
Line 166: | Line 206: | ||
PrintN(Str(n)) |
PrintN(Str(n)) |
||
CloseConsole()</syntaxhighlight> |
CloseConsole()</syntaxhighlight> |
||
==={{header|QB64}}=== |
|||
====Trial Division Method==== |
|||
<syntaxhighlight lang="qbasic">max=10001: n=1: p=0: t=Timer ' PRIMES.bas russian DANILIN |
|||
While n <= max ' 10001 104743 0.35 seconds |
|||
f=0: j=2 |
|||
While f < 1 |
|||
If j >= p ^ 0.5 Then f=2 |
|||
If p Mod j = 0 Then f=1 |
|||
j=j+1 |
|||
Wend |
|||
If f <> 1 Then n=n+1: ' Print n, p |
|||
p=p+1 |
|||
Wend |
|||
Print n-1, p-1, Timer-t</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10001 104743 0.35 seconds</pre> |
|||
====More Efficient TD Method==== |
|||
<syntaxhighlight lang="qbasic">'JRace's results: |
|||
'Original version: 10001 104743 .21875 |
|||
'This version: 10001 104743 .109375 |
|||
max = 10001: n=1: p=0: t = Timer ' PRIMES.bas |
|||
While n <= max ' 10001 104743 0.35 seconds |
|||
f = 0: j = 2 |
|||
pp = p^0.5 'NEW VAR: move exponentiation here, to outer loop |
|||
While f < 1 |
|||
' If j >= p ^ 0.5 Then f=2 'original line |
|||
' NOTE: p does not change in this loop, |
|||
' therefore p^0.5 doesn't change; |
|||
' moving p^0.5 to the outer loop will eliminate a lot of FP math) |
|||
If j >= pp Then f=2 'new version with exponentiation relocated |
|||
If p Mod j = 0 Then f=1 |
|||
j=j+1 |
|||
Wend |
|||
If f <> 1 Then n=n+1: ' Print n, p |
|||
p=p+1 |
|||
Wend |
|||
Print n-1, p-1, Timer - t</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10001 104743 0.11 seconds</pre> |
|||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
Line 396: | Line 477: | ||
<syntaxhighlight lang="fermat"> |
<syntaxhighlight lang="fermat"> |
||
Prime(10001); |
Prime(10001); |
||
</syntaxhighlight> |
|||
{{out}}<pre>104743</pre> |
|||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic"> |
|||
#include "isprime.bas" |
|||
function prime( n as uinteger ) as ulongint |
|||
if n=1 then return 2 |
|||
dim as integer p=3, pn=1 |
|||
while pn<n |
|||
if isprime(p) then pn + = 1 |
|||
p += 2 |
|||
wend |
|||
return p-2 |
|||
end function |
|||
print prime(10001) |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{out}}<pre>104743</pre> |
{{out}}<pre>104743</pre> |
||
Line 422: | Line 486: | ||
104743 |
104743 |
||
</pre> |
</pre> |
||
=={{header|GW-BASIC}}== |
|||
<syntaxhighlight lang="gwbasic">10 PN=1 |
|||
20 P = 3 |
|||
30 WHILE PN < 10001 |
|||
40 GOSUB 100 |
|||
50 IF Q = 1 THEN PN = PN + 1 |
|||
60 P = P + 2 |
|||
70 WEND |
|||
80 PRINT P-2 |
|||
90 END |
|||
100 REM tests if a number is prime |
|||
110 Q=0 |
|||
120 IF P = 2 THEN Q = 1: RETURN |
|||
130 IF P=3 THEN Q=1:RETURN |
|||
140 I=1 |
|||
150 I=I+2 |
|||
160 IF INT(P/I)*I = P THEN RETURN |
|||
170 IF I*I<=P THEN GOTO 150 |
|||
180 Q = 1 |
|||
190 RETURN</syntaxhighlight> |
|||
{{out}}<pre>104743</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 669: | Line 710: | ||
{{out}} |
{{out}} |
||
<pre>10001 104743 7 seconds</pre> |
<pre>10001 104743 7 seconds</pre> |
||
=={{header|QB64}}== |
|||
===Trial Division Method=== |
|||
<syntaxhighlight lang="qbasic">max=10001: n=1: p=0: t=Timer ' PRIMES.bas russian DANILIN |
|||
While n <= max ' 10001 104743 0.35 seconds |
|||
f=0: j=2 |
|||
While f < 1 |
|||
If j >= p ^ 0.5 Then f=2 |
|||
If p Mod j = 0 Then f=1 |
|||
j=j+1 |
|||
Wend |
|||
If f <> 1 Then n=n+1: ' Print n, p |
|||
p=p+1 |
|||
Wend |
|||
Print n-1, p-1, Timer-t</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10001 104743 0.35 seconds</pre> |
|||
===More Efficient TD Method=== |
|||
<syntaxhighlight lang="qbasic">'JRace's results: |
|||
'Original version: 10001 104743 .21875 |
|||
'This version: 10001 104743 .109375 |
|||
max = 10001: n=1: p=0: t = Timer ' PRIMES.bas |
|||
While n <= max ' 10001 104743 0.35 seconds |
|||
f = 0: j = 2 |
|||
pp = p^0.5 'NEW VAR: move exponentiation here, to outer loop |
|||
While f < 1 |
|||
' If j >= p ^ 0.5 Then f=2 'original line |
|||
' NOTE: p does not change in this loop, |
|||
' therefore p^0.5 doesn't change; |
|||
' moving p^0.5 to the outer loop will eliminate a lot of FP math) |
|||
If j >= pp Then f=2 'new version with exponentiation relocated |
|||
If p Mod j = 0 Then f=1 |
|||
j=j+1 |
|||
Wend |
|||
If f <> 1 Then n=n+1: ' Print n, p |
|||
p=p+1 |
|||
Wend |
|||
Print n-1, p-1, Timer - t</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10001 104743 0.11 seconds</pre> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
The 10001st prime number is the 10000th odd prime number. |
The 10001st prime number is the 10000th odd prime number. |
||