Piprimes: Difference between revisions

3,255 bytes added ,  4 months ago
m
(Piprimes in Dart)
m (→‎{{header|Wren}}: Minor tidy)
(6 intermediate revisions by 4 users not shown)
Line 346:
return true;
}</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
 
 
 
procedure ShowPiprimes(Memo: TMemo);
var N, P, Cnt: integer;
var S: string;
begin
N:= 0;
P:= 1;
Cnt:= 0;
S:='';
repeat
begin
S:=S+Format('%3D',[N]);
Inc(Cnt);
if (Cnt mod 10)=0 then S:=S+CRLF;
Inc(P);
if IsPrime(P) then N:= N+1;
end
until N >= 22;
Memo.Lines.Add(S);
end;
 
</syntaxhighlight>
{{out}}
<pre>
0 1 2 2 3 3 4 4 4 4
5 5 6 6 6 6 7 7 8 8
8 8 9 9 9 9 9 9 10 10
11 11 11 11 11 11 12 12 12 12
13 13 14 14 14 14 15 15 15 15
15 15 16 16 16 16 16 16 17 17
18 18 18 18 18 18 19 19 19 19
20 20 21 21 21 21 21 21
Elapsed Time: 1.328 ms.
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 408 ⟶ 474:
= 15= 15= 15= 15= 16= 16= 16= 16= 16= 16= 17= 17= 18= 18= 18= 18
= 18= 18= 19= 19= 19= 19= 20= 20= 21= 21= 21= 21= 21= 21</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight futurebasic"j">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
 
local fn Piprimes( limit as NSUInteger )
NSUInteger n = 0, p = 1
printf @"Piprimes from 1 through %lu:\n", limit
while ( n < limit )
printf @"%2lu \b", n
if p mod 10 == 0 then print
p++
if ( fn IsPrime(p) ) then n++
wend
end fn
 
fn Piprimes( 22 )
 
HandleEvents
</syntaxhighlight>
{{output}}}
<pre>
Piprimes from 1 through 22:
 
0 1 2 2 3 3 4 4 4 4
5 5 6 6 6 6 7 7 8 8
8 8 9 9 9 9 9 9 10 10
11 11 11 11 11 11 12 12 12 12
13 13 14 14 14 14 15 15 15 15
15 15 16 16 16 16 16 16 17 17
18 18 18 18 18 18 19 19 19 19
20 20 21 21 21 21 21 21
</pre>
 
=={{header|J}}==
Line 799 ⟶ 911:
</pre>
 
Pi primes ✔
=={{header|RPL}}==
{{works with|HP|49g}}
≪ 0
1 ROT '''FOR''' j j ISPRIME? + '''NEXT'''
≫ '<span style="color:blue">PI</span>' STO
≪ 0 → n
≪ { } 1 CF
'''DO'''
'n' INCR <span style="color:blue">PI</span>
'''IF''' DUP 22 ≤ '''THEN''' + '''ELSE''' DROP 1 SF '''END'''
'''UNTIL''' 1 FS? '''END'''
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 0. 1. 2. 2. 3. 3. 4. 4. 4. 4. 5. 5. 6. 6. 6. 6. 7. 7. 8. 8. 8. 8. 9. 9. 9. 9. 9. 9. 10. 10. 11. 11. 11. 11. 11. 11. 12. 12. 12. 12. 13. 13. 14. 14. 14. 14. 15. 15. 15. 15. 15. 15. 16. 16. 16. 16. 16. 16. 17. 17. 18. 18. 18. 18. 18. 18. 19. 19. 19. 19. 20. 20. 21. 21. 21. 21. 21. 21. }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
pi = 0
pies = (1..).lazy.map {|n| n.prime? ? pi += 1 : pi}.take_while{ pi < 22 }
pies.each_slice(10){|s| puts "%3d"*s.size % s}</syntaxhighlight>
{{out}}
<pre> 0 1 2 2 3 3 4 4 4 4
5 5 6 6 6 6 7 7 8 8
8 8 9 9 9 9 9 9 10 10
11 11 11 11 11 11 12 12 12 12
13 13 14 14 14 14 15 15 15 15
15 15 16 16 16 16 16 16 17 17
18 18 18 18 18 18 19 19 19 19
20 20 21 21 21 21 21 21
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">1..(prime(22)-1) -> map { .prime_count }.say</syntaxhighlight>
Line 808 ⟶ 955:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./seqfmt" for LstFmt
import "/fmt" for Fmt
 
var primes = Int.primeSieve(79) // go up to the 22nd
Line 829 ⟶ 974:
}
System.print("pi(n), the number of primes <= n, where n >= 1 and pi(n) < 22:")
for (chunk in Lst.chunks(pi, 10)) Fmt.printtprint("$2d", chunkpi, 10)
System.print("\nHighest n for this range = %(pi.count).")</syntaxhighlight>
 
9,482

edits