Long primes: Difference between revisions

Added Algol 68
(Undo revision 360477 by Chkas (talk))
(Added Algol 68)
 
(One intermediate revision by one other user not shown)
Line 123:
32000 is 1300
64000 is 2430
</pre>
 
=={{header|ALGOL 68}}==
The PERIOD operator is translated from the C sample's find_period routine.
<syntaxhighlight lang="algol68">
BEGIN # find some long primes - primes whose reciprocol have a period of p-1 #
INT max number = 64 000;
# sieve the primes to max number #
[ 1 : max number ]BOOL is prime; FOR i TO UPB is prime DO is prime[ i ] := ODD i OD;
is prime[ 1 ] := FALSE;
is prime[ 2 ] := TRUE;
FOR s FROM 3 BY 2 TO ENTIER sqrt( max number ) DO
IF is prime[ s ] THEN
FOR p FROM s * s BY s TO UPB is prime DO is prime[ p ] := FALSE OD
FI
OD;
 
OP PERIOD = ( INT n )INT: # returns the period of the reciprocal of n #
BEGIN
INT r := 1;
FOR i TO n + 1 DO
r *:= 10 MODAB n
OD;
INT rr = r;
INT period := 0;
WHILE r *:= 10 MODAB n;
period +:= 1;
r /= rr
DO SKIP OD;
period
END # PERIOD # ;
 
print( ( "Long primes upto 500:", newline, " " ) );
INT lp count := 0;
FOR p FROM 3 TO 500 DO
IF is prime[ p ] THEN
IF PERIOD p = p - 1 THEN
print( ( " ", whole( p, 0 ) ) );
lp count +:= 1
FI
FI
OD;
print( ( newline ) );
INT limit := 500;
FOR p FROM 500 WHILE limit <= 64 000 DO
IF p = limit THEN
print( ( "Long primes up to: ", whole( p, -5 ), ": ", whole( lp count, 0 ), newline ) );
limit *:= 2
FI;
IF is prime[ p ] THEN
IF PERIOD p = p - 1 THEN
lp count +:= 1
FI
FI
OD
 
END
</syntaxhighlight>
{{out}}
<pre>
Long primes upto 500:
7 17 19 23 29 47 59 61 97 109 113 131 149 167 179 181 193 223 229 233 257 263 269 313 337 367 379 383 389 419 433 461 487 491 499
Long primes up to: 500: 35
Long primes up to: 1000: 60
Long primes up to: 2000: 116
Long primes up to: 4000: 218
Long primes up to: 8000: 390
Long primes up to: 16000: 716
Long primes up to: 32000: 1300
Long primes up to: 64000: 2430
</pre>
 
Line 663 ⟶ 733:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Long_primes#Pascal Pascal].
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
if num mod 2 = 0 and num > 2
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
prim = 2
proc nextprim . .
repeat
prim += 1
until isprim prim = 1
.
.
func period n .
r = 1
repeat
r = (r * 10) mod n
p += 1
until r <= 1
.
return p
.
#
print "Long primes up to 500 are:"
repeat
nextprim
until prim > 500
if period prim = prim - 1
write prim & " "
cnt += 1
.
.
print ""
print ""
print "The number of long primes up to:"
limit = 500
repeat
if prim > limit
print limit & " is " & cnt
limit *= 2
.
until limit > 32000
if period prim = prim - 1
cnt += 1
.
nextprim
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
3,044

edits