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