Piprimes: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 15:
{{trans|Nim}}
<
I n == 2
R 1B
Line 34:
I pi == 22
L.break
print()</
{{out}}
Line 50:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
PROC Main()
Line 67:
FI
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Piprimes.png Screenshot from Atari 8-bit computer]
Line 77:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
# show pi(n) for n up to 21 #
INT max prime = 100; # guess of how large the primes we need are #
Line 91:
IF i MOD 10 = 0 THEN print( ( newline ) ) FI
OD
END</
{{out}}
<pre>
Line 106:
=={{header|Arturo}}==
<
piprimes: function [n] -> size select primes 'z [z =< n]
loop split.every: 10 select map 1..100 => piprimes => [& < 22] 'a ->
print map a => [pad to :string & 3]</
{{out}}
Line 124:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PIPRIMES.AWK
# converted from FreeBASIC
Line 151:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 168:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<
if v < 2 then return False
if v mod 2 = 0 then return v = 2
Line 186:
print running; " ";
end while
end</
{{out}}
<pre>
Line 193:
==={{header|FreeBASIC}}===
<
#include "isprime.bas"
Line 204:
loop
print : 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</pre>
==={{header|Tiny BASIC}}===
<
LET P = 0
10 IF N = 22 THEN END
Line 224:
LET I = I + 1
IF I*I <= P THEN GOTO 110
RETURN</
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 246:
print running using "##", " ";
loop
end</
{{out}}
<pre>
Line 254:
=={{header|C}}==
<
#include <stdlib.h>
Line 274:
}
return 0;
}</
{{out}}
Line 281:
=={{header|Cowgol}}==
<
sub isPrime(n: uint8): (r: uint8) is
Line 311:
end loop;
print_nl();
</syntaxhighlight>
{{out}}
Line 326:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// PiPrimes: Nigel Galloway. April 5th., 2021
let fN=let i=primes32() in Seq.unfold(fun(n,g,l)->Some(l,if n=g then (n+1,Seq.head i,l+1) else (n+1,g,l)))(1,Seq.head i,0)
fN|>Seq.takeWhile((>)22)|>Seq.chunkBySize 20|>Seq.iter(fun n->Array.iter(printf "%2d ") n; printfn "")
</syntaxhighlight>
{{out}}
<pre>
Line 341:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.primes.lists math.ranges math.statistics sequences ;
21 lprimes lnth [1,b) [ prime? ] cum-count
10 group [ [ "%2d " printf ] each nl ] each</
{{out}}
<pre>
Line 359:
=={{header|Fermat}}==
<
while n<22 do !n;!' ';p:=p+1;if Isprime(p)=1 then n:=n+1; fi; od</
{{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|FOCAL}}==
<
01.20 S N=1
01.30 T %3,C
Line 378:
02.30 I (I*I-N-1)2.4;S A=1;R
02.40 S A=N/I
02.50 I (FITR(A)-A)2.2;S A=0</
{{out}}
<pre>= 0= 1= 2= 2= 3= 3= 4= 4= 4= 4= 5= 5= 6= 6= 6= 6
Line 387:
=={{header|J}}==
<
{{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>
Line 394:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 426:
}
fmt.Printf("\n\nHighest n for this range = %d.\n", len(pi))
}</
{{out}}
Line 454:
'''Preliminaries'''
<
def emit_until(cond; stream):
Line 462:
if . == 2 then 3
else first(range(.+2; infinite; 2) | select(is_prime))
end;</
'''The task'''
<
def pi_primes:
foreach range(1; infinite) as $i ({n:0, np: 2}; # n counts, np is the next prime
Line 473:
.n);
emit_until(. >= 22; pi_primes)</
{{out}}
<pre>
Line 502:
=={{header|Julia}}==
<
function listpiprimes(maxpi)
Line 514:
listpiprimes(22)
</
<pre>
0 1 2 2 3 3 4 4 4 4
Line 527:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Multicolumn[pi, {Automatic, 10}, Appearance -> "Horizontal"]</
{{out}}
<pre>0 1 2 2 3 3 4 4 4 4
Line 540:
=={{header|Nim}}==
<
func isPrime(n: Natural): bool =
Line 562:
inc pi
if pi == 22: break
echo()</
Line 576:
=={{header|PARI/GP}}==
<
while( primepi( n ) < 22,
printf( "%3d", primepi(n) );
if( n++ % 10 == 1,
print()) )</
{{out}}
0 1 2 2 3 3 4 4 4 4
Line 593:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'state';
Line 599:
my @pi = map { state $pi = 0; $pi += is_prime $_ ? 1 : 0 } 1..1e4;
do { print shift(@pi) . ' ' } until $pi[0] >= 22;</
{{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|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ix</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
Line 618:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"pi[1..%d]:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)})</span>
<!--</
{{out}}
<pre>
Line 636:
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
<
1 - times
[ i 1+ isprime + ] ] is pi ( n --> n )
Line 642:
2 [ dup pi dup 22 < while
echo sp 1+ again ]
2drop</
{{out}}
Line 649:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
say @pi[^(@pi.first: * >= 22, :k)].batch(10)».fmt('%2d').join: "\n";</
{{out}}
<pre> 0 1 2 2 3 3 4 4 4 4
Line 663:
=={{header|REXX}}==
<
/*────────────────────────── where the pi function returns the number of primes ≤ N.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
Line 707:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 726:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 759:
see nl + "Found " + row + " Piprimes." + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 777:
=={{header|Sidef}}==
<
{{out}}
<pre>
Line 787:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "/seq" for Lst
import "/fmt" for Fmt
Line 807:
System.print("pi(n), the number of primes <= n, where n >= 1 and pi(n) < 22:")
for (chunk in Lst.chunks(pi, 10)) Fmt.print("$2d", chunk)
System.print("\nHighest n for this range = %(pi.count).")</
{{out}}
Line 825:
=={{header|XPL0}}==
<
int N, I;
[if N <= 1 then return false;
Line 842:
if IsPrime(P) then N:= N+1;
until N >= 22;
]</
{{out}}
|