Pandigital prime: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
imported>Grootson
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 3 users not shown)
Line 182:
<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}}==
Line 353 ⟶ 466:
7652413
</pre>
 
 
=={{header|FreeBASIC}}==
{{trans|Ring}}
<syntaxhighlight lang="freebasic">Function isPrime(Byval n As Integer) As Boolean
If n Mod 3 = 0 Then Return False
Dim As Integer i = 5
While i * i < n
If n Mod i = 0 Then Return False : End If : i += 2
If n Mod i = 0 Then Return False : End If : i += 4
Wend
Return True
End Function
 
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|Go}}==
Line 481 ⟶ 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 859 ⟶ 941:
The largest 0..7 pandigital prime is 76540231 20.30 ms
done...</pre>
 
=={{header|RPL}}==
Based on Factor's insights, we only need to check 4- and 7-digit pandigitals from biggest to smallest.
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}}==
Line 909 ⟶ 1,024:
<br>
This makes use of the optimization strategy in the Factor entry to do both the basic and optional tasks.
<syntaxhighlight lang="ecmascriptwren">import "./perm" for Perm
import "./math" for Int
import "./fmt" for Fmt
9,476

edits