One-two primes: Difference between revisions
m
→Generalized: A little less terse. Exact same functionality but better names and whitespace
SqrtNegInf (talk | contribs) (Added Perl) |
Thundergnat (talk | contribs) m (→Generalized: A little less terse. Exact same functionality but better names and whitespace) |
||
Line 689:
Limited the stretch to keep the run time reasonable. Finishes all in around 12 seconds on my system.
<syntaxhighlight lang="raku" line>for 929,(0,1),229,(1,2),930,(1,3),931,(1,4),932,(1,5),933,(1,6),934,(1,7),935,(1,8),
say "\nOEIS:A036{$oeis} - Smallest n digit prime using only {$
sub condense ($n) { $n.subst(/(.) {} :my $
sub build ($
▲ -> $oeis, $p {
▲ say "\nOEIS:A036{$oeis} - Smallest n digit prime using only {$p[0]} and {$p[1]} (or '0' if none exists):";
▲ sub condense ($n) { $n.subst(/(.) {} :my $r=$0; ($r**{9..*})/, -> $/ {"($0 x {1+$1.chars}) "}) }
▲ sub build ($d, $s='') { take $s and return unless $d; build($d-1,$s~$_) for |$p }
▲ sub get-prime ($d) {
▲ ($p[0] ?? (gather build $d).first: &is-prime
▲ !! (gather build $d-1, $p[1]).first: &is-prime
▲ ) // '0'
}
printf "%4d: %s\n", $_, condense .&get-prime for flat 1..20, 100, 200;
}</syntaxhighlight>
|