Pandigital prime: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|REXX}}: corrected test for primality.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(35 intermediate revisions by 19 users not shown)
Line 1:
{{Draft task|Prime Numbers}}
 
;Task:
Line 18:
=={{header|ALGOL 68}}==
Uses the observations in the Factor sample - the prime we are looking for can only have 7 or 4 digits.
<langsyntaxhighlight 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 #
Line 128:
find pd prime( 1, "pandigital" );
find pd prime( 0, "pandigital0" )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 135:
</pre>
 
Alternative, faster version {{Trans|Delphi}}
=={{header|C#|CSharp}}==
The Algol 68 FOR loop allows the loop counter to vary by values other than 1/-1, which makes ignoring even numbers easier... : )
<lang csharp>using System;
<syntaxhighlight lang="algol68">
FOR sp FROM 0 TO 1 DO
FOR x FROM IF sp = 1 THEN 7654321 ELSE 76543211 FI BY -2 TO 0 DO
IF x MOD 3 /= 0 THEN
STRING s = whole( x, 0 );
FOR ch FROM sp TO 7 DO IF NOT char in string( REPR ( ch + ABS "0" ), NIL, s ) THEN GOTO nxt FI OD;
INT i := 1;
WHILE i * i < x DO
IF x MOD ( i + 4 ) = 0 THEN GOTO nxt FI; i +:= 4;
IF x MOD ( i + 2 ) = 0 THEN GOTO nxt FI; i +:= 2
OD;
print( ( whole( sp, 0 ), "..7: ", whole( x, 0 ), newline ) ); GOTO done;
nxt: SKIP
FI
OD;
done: SKIP
OD
</syntaxhighlight>
 
{{out}}
class Program {
<pre>
// Find the highest pandigital number in base 10 (without the digit zero)
0..7: 76540231
1..7: 7652413
</pre>
 
=={{header|Arturo}}==
// Since the sum-of-digits of the pandigital numbers 1..9 and 1..8 are respectively 45 and 36, (both
<syntaxhighlight lang="arturo">allowed: @1..7
// divisible by 3 and therefore always composite), we will only be looking at pandigital numbers 1..7
pandigital?: function [n][
allowed = sort unique digits n
]
 
largestPossiblePandigital: 7654321
static void Main(string[] args) {
var sw = System.Diagnostics.Stopwatch.StartNew();
// The difference between every permutation is a multiple of 9. To check odds only, start at XXXXXX1
// and decrement by 18. It's slightly faster to check pan-digitality before the multi-factor test.
 
until -> dec 'largestPossiblePandigital [
for (int x = 7654321; ; x -= 18) {
and? -> pandigital? largestPossiblePandigital
-> prime? largestPossiblePandigital
]
 
print "The largest pandigital prime is:"
print largestPossiblePandigital</syntaxhighlight>
 
{{out}}
 
<pre>The largest pandigital prime is:
7652413</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">#include "isprime.kbs"
 
digits = 7654321
for z = 1 to 0 step -1
print "The largest"; z; "..7 pandigital prime is ";
start = msec
for n = digits to 0 step -18
cadena$ = string(n)
flag = True
for i = z to 7
if instr(cadena$, string(i)) = 0 then
flag = False
exit for
end if
next i
if flag and isPrime(n) then
print rjust(string(n), 8);". "; (msec - start); " ms"
exit for
end if
next n
digits = digits * 10 - 9
next z</syntaxhighlight>
 
==={{header|FreeBASIC}}===
{{trans|Ring}}
<syntaxhighlight lang="freebasic">#include "isprime.bas"
 
Dim As Integer digits = 7654321
For z As Integer = 1 To 0 Step -1
Print "The largest"; z; "..7 pandigital prime is ";
Dim As Double start = Timer
For n As Integer = digits To 0 Step -18
Dim As String cadena = Str(n)
Dim As Boolean flag = True
For i As Integer = z To 7
If Instr(cadena, Str(i)) = 0 Then
flag = False
Exit For
End If
Next i
If flag And isPrime(n) Then
Print Using "########. ##.## ms"; n; (Timer - start) * 10000
Exit For
End If
Next n
digits = digits * 10 - 9
Next z
Sleep</syntaxhighlight>
{{out}}
<pre>The largest 1..7 pandigital prime is 7652413. 6.32 ms
The largest 0..7 pandigital prime is 76540231. 13.95 ms</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Use "isprime.bas"
 
Public Sub Main()
Dim digits As Integer = 7654321
 
For z As Integer = 1 To 0 Step -1
Print "The largest "; z; "..7 pandigital prime is ";
For n As Integer = digits To 0 Step -18
Dim cadena As String = Str(n)
Dim flag As Boolean = True
For i As Integer = z To 7
If InStr(cadena, Str(i)) = 0 Then
flag = False
Break
End If
Next
If flag And isPrime(n) Then
Print Format$(Str$(n), "########"); ". "; Timer; " ms "
Break
End If
Next
digits = digits * 10 - 9
Next
End</syntaxhighlight>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">;XIncludeFile "isprime.pb"
 
OpenConsole()
digits.i = 7654321
For z.i = 1 To 0 Step -1
Print("The largest" + Str(z) + "..7 pandigital prime is ")
For n.i = digits To 0 Step -18
cadena.s = Str(n)
flag.b = #True
For i.i = z To 7
If FindString(cadena, Str(i)) = 0:
flag = #False
Break
EndIf
Next i
If flag And isPrime(n):
;Print ""; n; " "; (ElapsedMilliseconds() - start) * 10000; " ms"
PrintN(Str(n) + ". ")
Break
EndIf
Next n
digits = digits * 10 - 9
Next z
 
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
 
=={{header|C#|CSharp}}==
<syntaxhighlight lang="csharp">using System;
class Program {
// Find the highest pandigital number in base 10, excluding or including the digit zero.
// Since the sum-of-digits of the pandigital numbers 0..9 and 0..8 are respectively 45 and 36, (both
// divisible by 3 and therefore always composite), we will only be looking at pandigital numbers 0..7
static void fun(char sp) {
var sw = System.Diagnostics.Stopwatch.StartNew();
// The difference between every permutation is a multiple of 9. To check odds only,
// start at XXXXXX1 or XXXXXX01 and decrement by 18.
// It's slightly faster to check pan-digitality before the multi-factor test.
for (int x = sp == '1' ? 7654321 : 76543201; ; x -= 18) {
// Tests for pan-digitality of x
// Hard-coded to only checkCheck for digits 1sp through 7. If a duplicate occurs, at least one of the
// other required digits 1sp..7 will be missing, and therefore rejected.
var s = x.ToString();
for (var ch = '1'sp; ch < '8'; ch++) if (s.IndexOf(ch) < 0) goto bottomnxt;
 
// Multi-factor test
// There is no check for even numbers since starting on an odd number and stepping by an even number
if (x % 3 == 0) continue;
for (int i = 1; i * i < x; ) {
if (x % (i += 4) == 0) goto bottomnxt;
if (x % (i += 2) == 0) goto bottomnxt;
}
sw.Stop(); Console.WriteWriteLine("{0}..7: {1,10:n0} ns{2} μs", sp, x, sw.Elapsed.TotalMilliseconds * 1000); break;
bottomnxt: ;
}
}
 
}</lang>
static void Main(string[] args) {
{{out}}@ Tio.run:
fun('1');
<pre>7652413 29.2 ns</pre>
fun('0');
}
}</syntaxhighlight>
{{out|Output @ Tio.run}}
<pre>1..7: 7,652,413 21 μs
0..7: 76,540,231 24.5 μs</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
The original version generated results that weren't prime. This version has been rewritten to fix the prime problem and make it more modular.
 
 
<syntaxhighlight lang="Delphi">
{This code is normally put in a separate library, but it is included here for clarity}
 
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
 
 
function HighestPandigitalPrime(ZeroBased: boolean): integer;
{Returns the highest pandigital prime}
{ZeroBased = includes 0..N versus 1..N }
var I: integer;
 
type TDigitFlagArray = array [0..9] of integer;
 
procedure GetDigitCounts(N: integer; var FA: TDigitFlagArray);
{Get a count of all the digits in the number}
var T,I,DC: integer;
begin
DC:=Trunc(Log10(N))+1;
{Zero counts}
for I:=0 to High(FA) do FA[I]:=0;
{Count each time a digits is used}
for I:=0 to DC-1 do
begin
T:=N mod 10;
N:=N div 10;
Inc(FA[T]);
end;
end;
 
function IsPandigital(N: integer): boolean;
{Checks to see if all digits 0..N or 1..N are included}
var IA: TDigitFlagArray;
var I,DC: integer;
var Start: integer;
begin
Result:=False;
{ZeroBased includes zero}
if ZeroBased then Start:=0 else Start:=1;
{Get count of digits}
DC:=Trunc(Log10(N))+1;
{Get a count of each digits that are used}
GetDigitCounts(N,IA);
{Each digit 0..N or 1..N can only be used once}
for I:=Start to DC-1 do
if IA[I]<>1 then exit;
Result:=True;
end;
 
begin
if ZeroBased then Result:=76543210+1 else Result:=7654321;
{Check all numbers in the range}
while Result>2 do
begin
{Check that number is prime and Pandigital}
if IsPrime(Result) then
if IsPandigital(Result) then break;
Dec(Result,2);
end;
end;
 
 
 
procedure PandigitalPrime(Memo: TMemo);
var P: integer;
begin
P:=HighestPandigitalPrime(False);
Memo.Lines.Add(Format('Non Zero Based: %11.0n',[P+0.0]));
P:=HighestPandigitalPrime(True);
Memo.Lines.Add(Format('Zero Based: %11.0n',[P+0.0]));
end;
 
</syntaxhighlight>
{{out}}
<pre>
Non Zero Based: 7,652,413
Zero Based: 76,540,231
Elapsed Time: 6.044 ms.
 
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: io kernel math math.combinatorics math.functions
math.primes math.ranges present sequences sequences.cords ;
 
Line 186 ⟶ 460:
[ reverse 0 [ 10^ * + ] reduce-index prime? ] find-last nip
"The largest pandigital decimal prime is: " print
[ present write ] each nl</langsyntaxhighlight>
{{out}}
<pre>
Line 196 ⟶ 470:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 271 ⟶ 545:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 281 ⟶ 555:
76,540,231
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> gpdp=. >./@({:@(#~ 1&p:)@(10&#.@A.~ i.@!@#)\)
gpdp >: i. 7
7652413
gpdp i. 8
76540231</syntaxhighlight>
 
=={{header|jq}}==
Line 287 ⟶ 569:
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<langsyntaxhighlight lang="jq"># Output: a stream of strings of pandigital numbers
# drawing from the digits in the input array,
# in descending numerical order
Line 306 ⟶ 588:
| select(is_prime);
 
first(pandigital_primes)</langsyntaxhighlight>
{{out}}
<pre>
Line 313 ⟶ 595:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function pandigitals(Nfirstdig, lastdig)
mask = primesmask(10^N(lastdig - firstdig + 1))
for j in lastdig:-1:firstdig
ret = Int[]
for j in N:-1:1, in in= 10^j:-1:10^(j - firstdig + 1)
for i in evalpoly(10, firstdig:j):-1:evalpoly(10, j:-1:firstdig)
if mask[i]
dif = digits(mask[i)]
if length(d) == j && all(x -> count(y -> y == x, d) == 1, 1:jdigits(i)
push!if length(retd) == n && all(x -> count(y -> y == x, d) == 1, ifirstdig:j)
return i
end
end
end
end
return ret0
end
 
for firstdigit in [1, 0]
println("The largest prime containing numerals 1 through n of length n is ", maximum(pandigitals(9)))
println("Max pandigital prime over [$firstdigit, 9] is ", pandigitals(firstdigit, 9))
</lang>{{out}}<pre>The largest prime containing numerals 1 through n of length n is 7652413</pre>
end
</syntaxhighlight>{{out}}
<pre>
Max pandigital prime over [1, 9] is 7652413
Max pandigital prime over [0, 9] is 76540231
</pre>
 
=={{header|MATLAB}}==
'''including zero'''
<syntaxhighlight lang="matlab">
primeNumbers = sum(perms(0:7).*repmat((10*ones(1,8)).^(8-1:-1:0), [factorial(8),1]),'c')
mprintf('The largest pandigital prime is %u.', max(primeNumbers(find(members(primeNumbers, primes(7.7e7))==1))))
</syntaxhighlight>
'''without zero'''
<syntaxhighlight lang="matlab">
primeNumbers = sum(perms(1:7).*repmat((10*ones(1,7)).^(7-1:-1:0), [factorial(7),1]),'c')
mprintf('The largest pandigital prime is %u', max(primeNumbers(find(members(primeNumbers, primes(7.7e6))==1))))
</syntaxhighlight>
 
=={{header|Pascal}}==
=={{header|Free Pascal}}==
nearly {{Trans|Delphi}}
<syntaxhighlight lang="pascal">
program PandigitalPrime;
uses
SysUtils;
type
tDigits = set of 0..7;
const
cPanDigits : array['0'..'1'] of string=('76543210','7654321');
cDigits : array['0'..'1'] of tDigits =([0..7],[1..7]);
var
s : String;
x,i,l : NativeInt;
check,myCheck : tDigits;
sp : char;
begin
for sp := '0' to '1' do
Begin
myCheck := cDigits[sp];
val(cPanDigits[sp],x,i);
l := length(cPanDigits[sp]);
//only odd numbers
IF Not(Odd(x)) then
dec(x);
inc(x,2);
 
repeat
dec(x,2);
//early checking
if x mod 3 = 0 then
continue;
str(x,s);
if length(s)<l then
BREAK;
 
//check pandigital
check := myCheck;
For i := 1 to l do
Exclude(check,Ord(s[i])-Ord('0'));
if check <> [] then
continue;
 
//rest of prime check
i := 5;
repeat
if x mod i = 0 then BREAK;
Inc(i, 2);
if x mod i = 0 then BREAK;
Inc(i, 4);
until i * i >= x;
 
if i*i> x then
Begin
Writeln(Format('%s..7: %d', [sp, x]));
Break;
end;
until x <= 2;
 
end;
end.
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>0..7: 76540231
1..7: 7652413
</pre>
=={{header|Perl}}==
{{libheader|ntheory}}
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Pandigital_prime
Line 346 ⟶ 717:
is_prime($n) and exit ! print "$n\n";
} $digits;
}</langsyntaxhighlight>
{{out}}
<pre>7652413</pre>
 
7652413
Slightly different approach for optional portion of task.
</pre>
 
<syntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <forperm is_prime vecmax>;
 
my @p;
for my $c (0..7) {
forperm {
my $n = join '', @_;
push @p, $n if $n !~ /^0/ and is_prime($n);
} @{[0..$c]};
}
print vecmax(@p) . "\n";</syntaxhighlight>
{{out}}
<pre>76540231</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">avail</span>
Line 381 ⟶ 767:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
With full inner workings (the second "1" is really "01", a failing pandigital0), obviously removing the "?n" on the fourth line above will reduce the output to just four lines.<br>
Line 434 ⟶ 820:
76540231
Largest decimal pandigital0 prime with 8 digits:76,540,231
</pre>
 
=={{header|Quackery}}==
 
As per the Factor solution, only 4 and 7 digit pandigital numbers can be prime. We start with the largest 7 digit pandigital number and work down until we find one that is prime. (If there had been no 7 digit pandigital primes, I would then have added code for a 4 digit solution.) As <code>nextperm</code> generates permutations in lexicographical order the word <code>->revnum</code> subtracts each digit from 8 to give reverse numerical order.
 
<code>nextperm</code> is defined at [[Permutations with some identical elements#Quackery]].
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ 0 swap
witheach
[ 8 swap -
swap 10 * + ] ] is ->revnum ( [ --> n )
 
' [ [ 1 2 3 4 5 6 7 ]
[ 1 2 3 4 5 6 7 8 ] ]
witheach
[ [ dup ->revnum
isprime not while
nextperm again ]
->revnum
echo cr ]</syntaxhighlight>
 
{{out}}
 
<pre>7652413
76540231
</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>say ($_..7).reverse.permutations».join.first: &is-prime for 1,0;</syntaxhighlight>
<lang perl6>say max (1..9).map: -> $size {
|(1..$size).permutations».join.grep(&is-prime);
}</lang>
{{out}}
<pre>7652413</pre>
76540231</pre>
 
=={{header|REXX}}==
The longest part of the program execution time was the generating of 402 primes.
{{incorrect|REXX|7654321 is divisible by 19}}
 
<lang rexx>/*REXX program finds and displays the largest prime pandigital number. */
Essentially, the CPU time was displayed as using 0.00 seconds &nbsp; (rounded to two fractional decimal digits).
<syntaxhighlight lang="rexx">/*REXX program finds and displays the largest prime pandigital number. */
pand = reverse(123456789) /*get a big 9-digit pandigital number. */
gp= 0 /*indicate that primes not generated. */
Line 453 ⟶ 868:
call genP iSqrt($) /*gen primes up to $ (pandigital #). */
end
do k=$ by -12 for $ %2 /*start with $ and search downwards. */
if verify($, k)>0 then iterate /*$ pandigital? No, skip. _____ */
do d=1 for #; p= @.d /*divide by all the primes ≤ √ K */
Line 482 ⟶ 897:
end /*k*/ /* [↓] a prime (J) has been found. */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump #Ps; P──►@.assign P; P^2; P flag*/
end /*j*/; gp= 1; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 489 ⟶ 904:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">? "working..."
<lang ring>
hi = 7654321
load "stdlib.ring"
for z in ['1', '0']
see "working..." + nl
see "The largest " + z + "..7 pandigital prime is:" + nl"
st = clock()
for n = hi to 0 step -18
strn = string(n)
pandig = true
for i in z:'7'
if substr(strn, i) = 0
pandig = 0
exit
ok
next
if pandig and isprime(n)
et = clock()
? "" + n + " " + (et - st) / clockspersecond() * 1000 + " ms"
exit
ok
next
hi = hi * 10 - 9
next
put "done..."
func isprime(n)
if n % 3 = 0 return 0 ok
i = 5
while i * i < n
if n % i = 0 return 0 ok i += 2
if n % i = 0 return 0 ok i += 4
end
return 1</syntaxhighlight>
{{out|Output @ Tio.run}}
<pre>working...
The largest 1..7 pandigital prime is 7652413 9.84 ms
The largest 0..7 pandigital prime is 76540231 20.30 ms
done...</pre>
 
=={{header|RPL}}==
pand = 0
Based on Factor's insights, we only need to check 4- and 7-digit pandigitals from biggest to smallest.
limit = 7654321
Rather than generating permutations, we start from the biggest pandigital number for a given number of digits and go backwards by increment of 18, since:
* the difference between 2 pandigital numbers is a multiple of 9
* the biggest pandigital number for a given number of digits is odd and lower candidate numbers must also be odd
{{works with|HP|49}}
« R→I →STR DUP SIZE → d s
« 0
1 s '''FOR''' j
d j DUP SUB STR→ ALOG + '''NEXT''' <span style="color:grey">@ count digit occurrences into a unique number</span>
10 / 9 * 1 + LOG FP NOT <span style="color:grey">@ check that the result is a repunit</span>
» » '<span style="color:blue">ISPAND?</span>' STO
« 0
'''WHILE''' OVER '''REPEAT'''
10 * OVER + SWAP 1 - SWAP '''END'''
NIP 1 CF
DUP XPON ALOG '''FOR''' n
'''IF''' n ISPRIME? '''THEN'''
'''IF''' n <span style="color:blue">ISPAND?</span> '''THEN''' 1 SF n DUP XPON ALOG 'n' STO
'''END END'''
-18 '''STEP'''
'''IF''' 1 FC? '''THEN''' 0 '''END'''
» '<span style="color:blue">PANPRIME</span>' STO
« 7 <span style="color:blue">PANPRIME</span>
'''IF''' DUP NOT '''THEN''' 4 <span style="color:blue">PANPRIME</span> '''END'''
» 'P041' STO
{{out}}
<pre>
1: 7652413
</pre>
 
=={{header|Ruby}}==
for n = limit to 2 step -2
Using the observations from the Factor code:
flag = 1
<syntaxhighlight lang="ruby">require "prime"
strn = string(n)
if isprime(n)
def find_pan(ar) = ar.permutation(ar.size).find{|perm| perm.join.to_i.prime? }.join.to_i
for m = 1 to len(strn)
ind = count(strn,string(m))
digits = [7,6,5,4,3,2,1]
if ind != 1
puts find_pan(digits)
flag = 0
digits << 0
ok
puts find_pan(digits)</syntaxhighlight>
next
{{out}}
if flag = 1
<pre>7652413
pand = n
76540231
exit
</pre>
ok
ok
next
 
=={{header|Sidef}}==
see "" + pand + nl
<syntaxhighlight lang="ruby">func largest_pandigital_prime(base = 10, a = 1, b = base-1) {
 
for n in (b `downto` 1) {
see "done..." + nl
 
var digits = @(a..n -> flip)
func count(cString,dString)
 
sum = 0
if (base == 10) { # check for divisibility by 3
while substr(cString,dString) > 0
digits.sum++ % 3 == 0 && next
}
cString = substr(cString,substr(cString,dString)+len(string(sum)))
 
end
digits.permutations { |*p|
return sum
var v = p.flip.digits2num(base)
</lang>
return v if v.is_prime
}
}
 
return nil
}
 
say ("Max pandigital prime over [1, 9] is: ", largest_pandigital_prime(a: 1))
say ("Max pandigital prime over [0, 9] is: ", largest_pandigital_prime(a: 0))</syntaxhighlight>
{{out}}
<pre>
The largestMax pandigital prime over [1, 9] is: 7652413
Max pandigital prime over [0, 9] is: 76540231
7,652,413
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-perm}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<br>
This makes use of the optimization strategy in the Factor entry to do both the basic and optional tasks.
<langsyntaxhighlight ecmascriptlang="wren">import "./mathperm" for IntPerm
import "./fmtmath" for FmtInt
import "./fmt" for Fmt
 
// generates all permutations in lexicographical order
var permutations = Fn.new { |input|
var perms = [input]
var a = input.toList
var n = a.count - 1
for (c in 1...Int.factorial(n+1)) {
var i = n - 1
var j = n
while (a[i] > a[i+1]) i = i - 1
while (a[j] < a[i]) j = j - 1
a.swap(i, j)
j = n
i = i + 1
while (i < j) {
a.swap(i, j)
i = i + 1
j = j - 1
}
perms.add(a.toList)
}
return perms
}
 
for (start in 1..0) {
var outer = false
System.print("The largest pandigital decimal prime which uses all the digits %(start)..n once is:")
for (n in [7, 4]) {
var perms = permutationsPerm.calllistLex((start..n).toList)
for (i in perms.count - 1..0) {
if (perms[i][-1] % 2 == 0 || perms[i][-1] == 5 || (start == 0 && perms[i][0] == "0")) continue
Line 579 ⟶ 1,044:
if (outer) break
}
}</langsyntaxhighlight>
 
{{out}}
Line 588 ⟶ 1,053:
The largest pandigital decimal prime which uses all the digits 0..n once is:
76,540,231
</pre>
 
=={{header|XPL0}}==
The largest pandigital number not evenly divisible by 3 is 76543210. This
is because any number whose digits add to a multiple of 3 is evenly
divisible by 3, and 8+7+6+5+4+3+2+1+0 = 36 and adding 9 = 45, both of
which are evenly divisible by 3.
<syntaxhighlight lang "XPL0">func IsPrime(N); \Return 'true' if N is prime
int N, D;
[if N < 2 then return false;
if (N&1) = 0 then return N = 2;
if rem(N/3) = 0 then return N = 3;
D:= 5;
while D*D <= N do
[if rem(N/D) = 0 then return false;
D:= D+2;
if rem(N/D) = 0 then return false;
D:= D+4;
];
return true;
];
 
func Pandigital(N, Set); \Return 'true' if N is pandigital
int N, Set, Used;
[Used:= 0;
while N do
[N:= N/10;
if Used & 1<<rem(0) then return false;
Used:= Used ! 1<<rem(0);
];
return Used = Set;
];
 
int Data, Task, N, Set;
[Data:= ["1..7: ", 7654321, %1111_1110, "0..7: ", 76543210-1\odd\, %1111_1111];
for Task:= 0 to 1 do
[Text(0, Data(Task*3+0));
N:= Data(Task*3+1); Set:= Data(Task*3+2);
loop [if IsPrime(N) then
if Pandigital(N, Set) then
[IntOut(0, N); quit];
N:= N-2;
];
CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
1..7: 7652413
0..7: 76540231
</pre>
9,476

edits