Pandigital prime: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Now uses Wren-perm.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 18: | Line 18: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
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. |
||
< |
<syntaxhighlight 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 # |
# 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 # |
# considered: 1 is not prime, all 2, 3, 5, 6, 8 and 9 digit # |
||
Line 128: | Line 128: | ||
find pd prime( 1, "pandigital" ); |
find pd prime( 1, "pandigital" ); |
||
find pd prime( 0, "pandigital0" ) |
find pd prime( 0, "pandigital0" ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 136: | Line 136: | ||
=={{header|C#|CSharp}}== |
=={{header|C#|CSharp}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
class Program { |
class Program { |
||
Line 174: | Line 174: | ||
fun('0'); |
fun('0'); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out|Output @ Tio.run}} |
{{out|Output @ Tio.run}} |
||
<pre>1..7: 7,652,413 21 μs |
<pre>1..7: 7,652,413 21 μs |
||
Line 180: | Line 180: | ||
=={{header|Delphi|Delphi}}== |
=={{header|Delphi|Delphi}}== |
||
< |
<syntaxhighlight lang="pascal"> |
||
uses System.SysUtils, System.Classes, System.Math; |
uses System.SysUtils, System.Classes, System.Math; |
||
label nxt; |
label nxt; |
||
Line 197: | Line 197: | ||
end; |
end; |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 206: | Line 206: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: io kernel math math.combinatorics math.functions |
||
math.primes math.ranges present sequences sequences.cords ; |
math.primes math.ranges present sequences sequences.cords ; |
||
Line 218: | Line 218: | ||
[ reverse 0 [ 10^ * + ] reduce-index prime? ] find-last nip |
[ reverse 0 [ 10^ * + ] reduce-index prime? ] find-last nip |
||
"The largest pandigital decimal prime is: " print |
"The largest pandigital decimal prime is: " print |
||
[ present write ] each nl</ |
[ present write ] each nl</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 228: | Line 228: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|Ring}} |
{{trans|Ring}} |
||
< |
<syntaxhighlight lang="freebasic">Function isPrime(Byval n As Integer) As Boolean |
||
If n Mod 3 = 0 Then Return False |
If n Mod 3 = 0 Then Return False |
||
Dim As Integer i = 5 |
Dim As Integer i = 5 |
||
Line 258: | Line 258: | ||
digits = digits * 10 - 9 |
digits = digits * 10 - 9 |
||
Next z |
Next z |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The largest 1..7 pandigital prime is 7652413. 6.32 ms |
<pre>The largest 1..7 pandigital prime is 7652413. 6.32 ms |
||
Line 267: | Line 267: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 342: | Line 342: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 358: | Line 358: | ||
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`. |
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`. |
||
< |
<syntaxhighlight lang="jq"># Output: a stream of strings of pandigital numbers |
||
# drawing from the digits in the input array, |
# drawing from the digits in the input array, |
||
# in descending numerical order |
# in descending numerical order |
||
Line 377: | Line 377: | ||
| select(is_prime); |
| select(is_prime); |
||
first(pandigital_primes)</ |
first(pandigital_primes)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 384: | Line 384: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
function pandigitals(firstdig, lastdig) |
function pandigitals(firstdig, lastdig) |
||
Line 405: | Line 405: | ||
println("Max pandigital prime over [$firstdigit, 9] is ", pandigitals(firstdigit, 9)) |
println("Max pandigital prime over [$firstdigit, 9] is ", pandigitals(firstdigit, 9)) |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Max pandigital prime over [1, 9] is 7652413 |
Max pandigital prime over [1, 9] is 7652413 |
||
Line 413: | Line 413: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Pandigital_prime |
use strict; # https://rosettacode.org/wiki/Pandigital_prime |
||
Line 426: | Line 426: | ||
is_prime($n) and exit ! print "$n\n"; |
is_prime($n) and exit ! print "$n\n"; |
||
} $digits; |
} $digits; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>7652413</pre> |
<pre>7652413</pre> |
||
Line 432: | Line 432: | ||
Slightly different approach for optional portion of task. |
Slightly different approach for optional portion of task. |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use ntheory <forperm is_prime vecmax>; |
use ntheory <forperm is_prime vecmax>; |
||
Line 443: | Line 443: | ||
} @{[0..$c]}; |
} @{[0..$c]}; |
||
} |
} |
||
print vecmax(@p) . "\n";</ |
print vecmax(@p) . "\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>76540231</pre> |
<pre>76540231</pre> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">avail</span> |
||
Line 476: | Line 476: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{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> |
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 532: | Line 532: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>for 1, 0 -> $i { |
||
say max ($i..7).map: -> $size { |
say max ($i..7).map: -> $size { |
||
|($i..$size).permutations».join.grep(&is-prime); |
|($i..$size).permutations».join.grep(&is-prime); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>7652413 |
<pre>7652413 |
||
Line 545: | Line 545: | ||
Essentially, the CPU time was displayed as using 0.00 seconds (rounded to two fractional decimal digits). |
Essentially, the CPU time was displayed as using 0.00 seconds (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. */ |
pand = reverse(123456789) /*get a big 9-digit pandigital number. */ |
||
gp= 0 /*indicate that primes not generated. */ |
gp= 0 /*indicate that primes not generated. */ |
||
Line 582: | Line 582: | ||
end /*k*/ /* [↓] a prime (J) has been found. */ |
end /*k*/ /* [↓] a prime (J) has been found. */ |
||
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump #Ps; P──►@.assign P; P^2; P flag*/ |
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump #Ps; P──►@.assign P; P^2; P flag*/ |
||
end /*j*/; gp= 1; return</ |
end /*j*/; gp= 1; return</syntaxhighlight> |
||
{{out|output|text= when using the internal default input:}} |
{{out|output|text= when using the internal default input:}} |
||
<pre> |
<pre> |
||
Line 589: | Line 589: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring">? "working..." |
||
hi = 7654321 |
hi = 7654321 |
||
for z in ['1', '0'] |
for z in ['1', '0'] |
||
Line 620: | Line 620: | ||
if n % i = 0 return 0 ok i += 4 |
if n % i = 0 return 0 ok i += 4 |
||
end |
end |
||
return 1</ |
return 1</syntaxhighlight> |
||
{{out|Output @ Tio.run}} |
{{out|Output @ Tio.run}} |
||
<pre>working... |
<pre>working... |
||
Line 629: | Line 629: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Using the observations from the Factor code: |
Using the observations from the Factor code: |
||
< |
<syntaxhighlight lang="ruby">require "prime" |
||
def find_pan(ar) = ar.permutation(ar.size).find{|perm| perm.join.to_i.prime? }.join.to_i |
def find_pan(ar) = ar.permutation(ar.size).find{|perm| perm.join.to_i.prime? }.join.to_i |
||
Line 636: | Line 636: | ||
puts find_pan(digits) |
puts find_pan(digits) |
||
digits << 0 |
digits << 0 |
||
puts find_pan(digits)</ |
puts find_pan(digits)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>7652413 |
<pre>7652413 |
||
Line 643: | Line 643: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func largest_pandigital_prime(base = 10, a = 1, b = base-1) { |
||
for n in (b `downto` 1) { |
for n in (b `downto` 1) { |
||
Line 663: | Line 663: | ||
say ("Max pandigital prime over [1, 9] is: ", largest_pandigital_prime(a: 1)) |
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))</ |
say ("Max pandigital prime over [0, 9] is: ", largest_pandigital_prime(a: 0))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 676: | Line 676: | ||
<br> |
<br> |
||
This makes use of the optimization strategy in the Factor entry to do both the basic and optional tasks. |
This makes use of the optimization strategy in the Factor entry to do both the basic and optional tasks. |
||
< |
<syntaxhighlight lang="ecmascript">import "./perm" for Perm |
||
import "./math" for Int |
import "./math" for Int |
||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
Line 696: | Line 696: | ||
if (outer) break |
if (outer) break |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |