Pandigital prime: Difference between revisions

→‎{{header|ALGOL 68}}: Tweaked code and added solution for digits 0..n
m (→‎{{header|Phix}}: added a remove "?n" note)
(→‎{{header|ALGOL 68}}: Tweaked code and added solution for digits 0..n)
Line 17:
 
=={{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.
<lang algol68>BEGIN # Findpermutation thecode largestfrom n-digitthe primeAlgol that68 containsPermutations allby theswapping task digits 1..n #
# As noted in the Factor sample, only 7 and 4 digit primes need be #
# considered: 1 is not prime, all 2, 3, 5, 6, 8 and 9 digit #
# pandigital numbers are divisible 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 28 ⟶ 25:
PROC generate = ( INT k, REF[]INT a, REF[]INT p, REF INT p pos )VOID:
IF k = 1 THEN
INT alast valuedigit := a[ 0UPB a ];
FORIF dlast TOdigit UPB/= a0 DOAND last digit /= 2 AND last digit /= 5 THEN
a value# *:=the 10number +:=doesn't a[end din ]2 or 5 so might be prime #
OD INT a value := a[ 0 ];
p[ p pos ] :=FOR d TO UPB a value;DO
p pos a value *:= 10 +:= 1a[ d ]
FIOD;
p[ p pos +:= 1 pd prime] := pa value
FI
ELSE
# Generate permutations with kth unaltered #
Line 49:
FI # generate # ;
# generate permutations of a, p is used to hold the output #
# returns the number of permutations stored #
PROC permute digits = ( REF[]INT a, REF[]INT p )VOIDINT:
BEGIN
INT p pos := 0-1;
generate( ( UPB a + 1 ) - LWB a, a[ AT 0 ], p[ AT 0 ], p pos );
p pos
END # permute digits # ;
# Quicksorts in-place the array of integers a, from lb to ub #
Line 76 ⟶ 78:
quicksort( a, left, ub )
FI # quicksort # ;
# attenmpt to find the maximum n digit pandigital prime with digits f..n, return it if found, 0 otherwise #
PROC try pd prime = ( INT f, INT n )INT:
BEGIN
# array of digits to permute for the numbers #
[ 0f : n - 1 ]INT digits; FOR i FROM LWB digits TO nUPB digits DO digits[ i - 1 ] := i OD;
# 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;
FOR i FROM 2 TO n DO factorial n *:= i OD;
[ 0 : factorial n - 1 ]INT permuted digits;
# permute the digits #
INT p count = permute digits( digits, permuted digits );
# sort the permuted numbers, assuming the prime is near the high end #
quicksort( permuted digits, LWB permuted digits, UPBp permuted digitscount );
# try finding a prime - use trial division to test for primality #
INT pd prime := 0;
FOR p pos FROM UPBp permuted digitscount BY -1 TO LWB permuted digits WHILE pd prime = 0 DO
INT p = permuted digits[ p pos ];
IF# ODDwe phave ANDonlt pstored MODthe 10odd /=numbers that don't end in 5 THEN#
# and we know BOOLthey primeare :=not TRUE;divisible by 3 #
BOOL prime := FOR i FROM 3 BY 2 TO ENTIER sqrt(p)TRUE;
FOR i FROM 7 BY 2 TO ENTIER sqrt(p) WHILE prime := p MOD i /= 0 DO SKIP OD;
IF prime DO SKIP OD;THEN
IF# found a pandigital prime THEN#
pd prime := # found a pandigital prime #p
pd prime := p
FI
FI
OD;
pd prime
END # try pd prime # ;
</lang>
 
===Digits 1 to n===
<lang algol68>BEGIN # Find the largest n-digit prime that contains all the digits 1..n #
# As noted in the Factor sample, only 7 and 4 digit primes need be #
# considered: 1 is not prime, all 2, 3, 5, 6, 8 and 9 digit #
# pandigital numbers are divisible by 3 #
# 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 #
IF INT pd prime := try pd prime( 1, 7 );
pd prime > 0
THEN
print( ( "max pandigital prime: ", whole( pd prime, 0 ), newline ) )
ELIF pd prime := try pd prime( 1, 4 );
pd prime > 0
THEN
Line 118 ⟶ 125:
print( ( "Can't find a pandigital prime", newline ) )
FI
END</lang>
</lang>
{{out}}
<pre>
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.
<lang algol68>BEGIN # Find the largest n-digit prime that contains all the digits 1..n #
# As noted in the Factor sample, only 8 and 5 digit primes need be #
# considered: 10 is not prime, all 3, 4, 6, 7 and 9 digit #
# pandigital numbers are divisible by 3 #
# insert the common code here #
# first try an 8 digit number then a 5 digit number if we can't find an 8 digit one #
IF INT pd prime := try pd prime( 0, 7 );
pd prime > 0
THEN
print( ( "max pandigital prime: ", whole( pd prime, 0 ), newline ) )
ELIF pd prime := try pd prime( 0, 4 );
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>
max pandigital prime: 76540231
</pre>
 
3,044

edits