One-two primes: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 19:
 
 
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
As more than 64 bit numbers are required, this uses Algol 68G's LONG LONG INT which has programmer specified precision. The default precision is sufficient for this task.
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">
BEGIN # find the lowest 1-2 primes with n digits, 1-2 primes contain the #
# digits 1 and 2 only #
 
PR read "primes.incl.a68" PR # include prime utilities #
[ 1 : 20 ]CHAR v12; # will hold the digits of the 1-2 prime #
FOR i TO UPB v12 DO v12[ i ] := "0" OD; # in little endian format #
BOOL v12 overflowed := FALSE;
 
# initialises v12 to digit count 1s #
PROC init v12 = ( INT digit count )VOID:
BEGIN
v12 overflowed := FALSE;
FOR digit TO digit count DO
v12[ digit ] := "1"
OD
END # init v12 # ;
# sets v12 to value - the digits corresponding to the set bits of value #
# sre set to 2, the others 1 #
PROC set v12 = ( INT digit count, INT value )VOID:
BEGIN
INT v := value;
FOR digit TO digit count WHILE v > 0 DO
v12[ digit ] := IF ODD v THEN "2" ELSE "1" FI;
v OVERAB 2
OD;
v12 overflowed := v /= 0
END # set v12 # ;
 
# converts v12 to a numeric valiue #
PROC v12 value = ( INT digit count )LONG LONG INT:
BEGIN
LONG LONG INT n12 := 0;
FOR digit FROM digit count BY -1 TO LWB v12 DO
n12 *:= 10 +:= IF v12[ digit ] = "2" THEN 2 ELSE 1 FI
OD;
n12
END # v12 value # ;
 
# returns TRUE if the value in v12 is prime, FALSE otherwise #
PROC v12 is prime = ( INT digit count )BOOL: is probably prime( v12 value( digit count ) );
 
# show the first 20 1-2 primes #
FOR digits TO 20 DO
init v12( digits );
# 2 can only be the final digit of the 1 digit 1-2 prime #
# for all other 1-2 primes, the final digit can't be 2 #
FOR v FROM IF digits = 1 THEN 1 ELSE 2 FI BY 2 WHILE NOT v12 is prime( digits )
AND NOT v12 overflowed
DO
set v12( digits, v )
OD;
print( ( whole( digits, -5 ), ": " ) );
IF v12 overflowed THEN
# couldn't find a prime #
print( ( "-1" ) )
ELSE
# found a prime #
FOR digit FROM digits BY -1 TO LWB v12 DO
print( ( v12[ digit ] ) )
OD
FI;
print( ( newline ) )
OD
 
END
</syntaxhighlight>
{{out}}
<pre>
1: 2
2: 11
3: 211
4: 2111
5: 12211
6: 111121
7: 1111211
8: 11221211
9: 111112121
10: 1111111121
11: 11111121121
12: 111111211111
13: 1111111121221
14: 11111111112221
15: 111111112111121
16: 1111111112122111
17: 11111111111112121
18: 111111111111112111
19: 1111111111111111111
20: 11111111111111212121
</pre>
 
=={{header|C}}==
3,048

edits