Numbers whose binary and ternary digit sums are prime: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 5:
<br><br>
 
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find numbers whose digit sums in binary and ternary are prime #
# reurns a sieve of primes up to n #
PROC sieve = ( INT n )[]BOOL:
BEGIN
[ 1 : n ]BOOL p;
p[ 1 ] := FALSE; p[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO n DO p[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO n DO p[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( n ) DO
IF p[ i ] THEN FOR s FROM i * i BY i + i TO n DO p[ s ] := FALSE OD FI
OD;
p
END # prime list # ;
# returns the digit sum of n in base b #
PRIO DIGITSUM = 9;
OP DIGITSUM = ( INT n, b )INT:
BEGIN
INT d sum := 0;
INT v := ABS n;
WHILE v > 0 DO
d sum +:= v MOD b;
v OVERAB b
OD;
d sum
END # DIGITSUM # ;
INT max number = 200;
[]BOOL prime = sieve( max number );
INT n count := 0;
FOR n TO max number DO
INT d sum 2 = n DIGITSUM 2;
IF prime[ d sum 2 ] THEN
INT d sum 3 = n DIGITSUM 3;
IF prime[ d sum 3 ] THEN
# the base 2 and base 3 digit sums of n are both prime #
print( ( " ", whole( n, -3 ), IF prime[ n ] THEN "*" ELSE " " FI ) );
n count +:= 1;
IF n count MOD 14 = 0 THEN print( ( newline ) ) FI
FI
FI
OD;
print( ( newline ) );
print( ( "Found ", whole( n count, 0 ), " numbers whose binary and ternary digit sums are prime", newline ) );
print( ( " those that are themselves prime are suffixed with a ""*""", newline ) )
END</lang>
{{out}}
<pre>
5* 6 7* 10 11* 12 13* 17* 18 19* 21 25 28 31*
33 35 36 37* 41* 47* 49 55 59* 61* 65 67* 69 73*
79* 82 84 87 91 93 97* 103* 107* 109* 115 117 121 127*
129 131* 133 137* 143 145 151* 155 157* 162 167* 171 173* 179*
181* 185 191* 193* 199*
Found 61 numbers whose binary and ternary digit sums are prime
those that are themselves prime are suffixed with a "*"
</pre>
 
=={{header|Factor}}==
3,049

edits