Pandigital prime: Difference between revisions
Content added Content deleted
(Added Go) |
(→{{header|ALGOL 68}}: Combine the 1..n and 0..n solutions into one) |
||
Line 17: | Line 17: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
===Common code=== |
|||
Uses the observations in the Factor sample - the prime we are looking for can only have 7 or 4 digits. |
Uses the observations in the Factor sample - the prime we are looking for can only have 7 or 4 digits. |
||
<lang algol68> |
<lang algol68>BEGIN # Find the largest n-digit prime that contains all the digits 1..n # |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
# 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 # |
# entry - uses Heap's algorithm - based on the pseudo code on the # |
||
# Wikipedia page for Heap's Algorithm # |
# Wikipedia page for Heap's Algorithm # |
||
Line 83: | Line 89: | ||
# array of digits to permute for the numbers # |
# 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; |
[ 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 |
# array to hold the permuted digits, there will be ( ( n + 1 ) - f)! elements # |
||
INT factorial n := 1; FOR i FROM 2 TO ( n + 1 ) - f DO factorial n *:= i OD; |
INT factorial n := 1; FOR i FROM 2 TO ( n + 1 ) - f DO factorial n *:= i OD; |
||
[ 0 : factorial n - 1 ]INT permuted digits; |
[ 0 : factorial n - 1 ]INT permuted digits; |
||
Line 105: | Line 111: | ||
pd prime |
pd prime |
||
END # try pd prime # ; |
END # try pd prime # ; |
||
# trys to find the maximem pandigital/pandigital0 prime # |
|||
⚫ | |||
PROC find pd prime = ( INT first digit, STRING title )VOID: |
|||
⚫ | |||
===Digits 1 to n=== |
|||
⚫ | |||
<lang algol68>BEGIN # Find the largest n-digit prime that contains all the digits 1..n # |
|||
pd prime > 0 |
|||
⚫ | |||
⚫ | |||
print( ( "max ", title, " prime: ", whole( pd prime, 0 ), newline ) ) |
|||
⚫ | |||
# insert the common code here # |
|||
⚫ | |||
# first try a 7 digit number then a 4 digit number if we can't find a 7 digit one # |
|||
⚫ | |||
⚫ | |||
print( ( "max ", title, " prime: ", whole( pd prime, 0 ), newline ) ) |
|||
ELSE |
|||
print( ( " |
print( ( "Can't find a ", title, " prime", newline ) ) |
||
FI # find pd prime # ; |
|||
# task # |
|||
find pd prime( 1, "pandigital" ); |
|||
⚫ | |||
find pd prime( 0, "pandigital0" ) |
|||
⚫ | |||
ELSE |
|||
print( ( "Can't find a pandigital prime", newline ) ) |
|||
FI |
|||
END |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
max pandigital prime: 7652413 |
max pandigital prime: 7652413 |
||
⚫ | |||
</pre> |
|||
===Digits 0 to n=== |
|||
Also uses the observations in the Factor sample - the prime we are looking for can only have 8 or 5 digits. |
|||
⚫ | |||
⚫ | |||
# considered: 10 is not prime, all 3, 4, 6, 7 and 9 digit # |
|||
⚫ | |||
# insert the common code here # |
|||
⚫ | |||
IF INT pd prime := try pd prime( 0, 7 ); |
|||
⚫ | |||
⚫ | |||
print( ( "max pandigital prime: ", whole( pd prime, 0 ), newline ) ) |
|||
⚫ | |||
pd prime > 0 |
|||
THEN |
|||
print( ( "max pandigital prime: ", whole( pd prime, 0 ), newline ) ) |
|||
ELSE |
|||
print( ( "Can't find a pandigital prime", newline ) ) |
|||
FI |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
⚫ | |||
</pre> |
</pre> |
||