Pandigital prime: Difference between revisions
→{{header|ALGOL 68}}: Combine the 1..n and 0..n solutions into one
(Added Go) |
(→{{header|ALGOL 68}}: Combine the 1..n and 0..n solutions into one) |
||
Line 17:
=={{header|ALGOL 68}}==
Uses the observations in the Factor sample - the prime we are looking for can only have 7 or 4 digits.
<lang algol68>
# considered: 1 is not prime, all 2, 3, 5, 6, 8 and 9 digit #▼
# digits 0..n (only 5 and 8 digit numbers need be considered as #
# the 0 digit does not affect divisibility by 3) #
# permutation code from the Algol 68 Permutations by swapping task #
# entry - uses Heap's algorithm - based on the pseudo code on the #
# Wikipedia page for Heap's Algorithm #
Line 83 ⟶ 89:
# array of digits to permute for the numbers #
[ f : n ]INT digits; FOR i FROM LWB digits TO UPB digits DO digits[ i ] := i OD;
# array to hold the permuted digits, there will be ( ( n + 1 ) - f
INT factorial n := 1; FOR i FROM 2 TO ( n + 1 ) - f DO factorial n *:= i OD;
[ 0 : factorial n - 1 ]INT permuted digits;
Line 105 ⟶ 111:
pd prime
END # try pd prime # ;
# trys to find the maximem pandigital/pandigital0 prime #
</lang>▼
PROC find pd prime = ( INT first digit, STRING title )VOID:
IF # first try
THEN▼
▲ # considered: 1 is not prime, all 2, 3, 5, 6, 8 and 9 digit #
pd prime > 0▼
THEN▼
▲ IF INT pd prime := try pd prime( 1, 7 );
print( ( "
# task
find pd prime( 1, "pandigital" );
▲ THEN
find
▲END</lang>
{{out}}
<pre>
max pandigital prime: 7652413
▲<lang algol68>BEGIN # Find the largest n+1 digit prime that contains all the digits 0..n #
▲ # As noted in the Factor sample, only 8 and 5 digit primes need be #
▲ # pandigital numbers are divisible by 3 #
▲ # first try an 8 digit number then a 5 digit number if we can't find an 8 digit one #
▲ pd prime > 0
▲ THEN
▲ ELIF pd prime := try pd prime( 0, 4 );
▲max pandigital prime: 76540231
</pre>
|