Pandigital prime: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Wren}}: Now uses Wren-perm.)
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.
<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 136:
 
=={{header|C#|CSharp}}==
<langsyntaxhighlight lang="csharp">using System;
class Program {
Line 174:
fun('0');
}
}</langsyntaxhighlight>
{{out|Output @ Tio.run}}
<pre>1..7: 7,652,413 21 μs
Line 180:
 
=={{header|Delphi|Delphi}}==
<langsyntaxhighlight lang="pascal">
uses System.SysUtils, System.Classes, System.Math;
label nxt;
Line 197:
end;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 206:
=={{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 218:
[ reverse 0 [ 10^ * + ] reduce-index prime? ] find-last nip
"The largest pandigital decimal prime is: " print
[ present write ] each nl</langsyntaxhighlight>
{{out}}
<pre>
Line 228:
=={{header|FreeBASIC}}==
{{trans|Ring}}
<langsyntaxhighlight lang="freebasic">Function isPrime(Byval n As Integer) As Boolean
If n Mod 3 = 0 Then Return False
Dim As Integer i = 5
Line 258:
digits = digits * 10 - 9
Next z
Sleep</langsyntaxhighlight>
{{out}}
<pre>The largest 1..7 pandigital prime is 7652413. 6.32 ms
Line 267:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 342:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 358:
 
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 377:
| select(is_prime);
 
first(pandigital_primes)</langsyntaxhighlight>
{{out}}
<pre>
Line 384:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function pandigitals(firstdig, lastdig)
Line 405:
println("Max pandigital prime over [$firstdigit, 9] is ", pandigitals(firstdigit, 9))
end
</langsyntaxhighlight>{{out}}
<pre>
Max pandigital prime over [1, 9] is 7652413
Line 413:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Pandigital_prime
Line 426:
is_prime($n) and exit ! print "$n\n";
} $digits;
}</langsyntaxhighlight>
{{out}}
<pre>7652413</pre>
Line 432:
Slightly different approach for optional portion of task.
 
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <forperm is_prime vecmax>;
Line 443:
} @{[0..$c]};
}
print vecmax(@p) . "\n";</langsyntaxhighlight>
{{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 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>
<!--</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 532:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>for 1, 0 -> $i {
say max ($i..7).map: -> $size {
|($i..$size).permutations».join.grep(&is-prime);
}
}</langsyntaxhighlight>
{{out}}
<pre>7652413
Line 545:
 
Essentially, the CPU time was displayed as using 0.00 seconds &nbsp; (rounded to two fractional decimal digits).
<langsyntaxhighlight 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 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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 589:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">? "working..."
hi = 7654321
for z in ['1', '0']
Line 620:
if n % i = 0 return 0 ok i += 4
end
return 1</langsyntaxhighlight>
{{out|Output @ Tio.run}}
<pre>working...
Line 629:
=={{header|Ruby}}==
Using the observations from the Factor code:
<langsyntaxhighlight lang="ruby">require "prime"
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)</langsyntaxhighlight>
{{out}}
<pre>7652413
Line 643:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func largest_pandigital_prime(base = 10, a = 1, b = base-1) {
 
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))</langsyntaxhighlight>
{{out}}
<pre>
Line 676:
<br>
This makes use of the optimization strategy in the Factor entry to do both the basic and optional tasks.
<langsyntaxhighlight lang="ecmascript">import "./perm" for Perm
import "./math" for Int
import "./fmt" for Fmt
Line 696:
if (outer) break
}
}</langsyntaxhighlight>
 
{{out}}
10,327

edits