One-two primes: Difference between revisions

m
→‎Generalized: A little less terse. Exact same functionality but better names and whitespace
(Added Perl)
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),
229936,(1,29),930937,(12,3),931938,(12,47),932939,(12,59),933940,(13,64),934941,(13,75),935942,(13,87),936943,(13,98),
937944,(24,37),938945,(24,9),946,(5,7),939947,(25,9),948,(6,7),949,(7,8),950,(7,9),951,(8,9)
-> $oeis, $ppair {
940,(3,4),941,(3,5),942,(3,7),943,(3,8),
 
944,(4,7),945,(4,9),
say "\nOEIS:A036{$oeis} - Smallest n digit prime using only {$ppair[0]} and {$ppair[1]} (or '0' if none exists):";
946,(5,7),947,(5,9),
 
948,(6,7),
sub condense ($n) { $n.subst(/(.) {} :my $rrepeat=$0; ($rrepeat**{9..*})/, -> $/ {"($0 x {1+$1.chars}) "}) }
949,(7,8),950,(7,9),
 
951,(8,9)
sub build ($ddigit, $ssofar='') { take $ssofar and return unless $ddigit; build($ddigit-1,$ssofar~$_) for |$ppair }
-> $oeis, $p {
 
say "\nOEIS:A036{$oeis} - Smallest n digit prime using only {$p[0]} and {$p[1]} (or '0' if none exists):";
sub get-prime ($ddigits) {
sub condense ($n) { $n.subst(/(.) {} :my $r=$0; ($r**{9..*})/, -> $/ {"($0 x {1+$1.chars}) "}) }
($ppair[0] ?? (gather build $ddigits).first: &is-prime
sub build ($d, $s='') { take $s and return unless $d; build($d-1,$s~$_) for |$p }
!! (gather build $ddigits-1, $ppair[1]).first: &is-prime
sub get-prime ($d) {
) // '0'
($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>
10,333

edits