Truncatable primes: Difference between revisions

→‎{{header|Perl 6}}: replace with much faster version
(Number base is 10)
(→‎{{header|Perl 6}}: replace with much faster version)
Line 1,095:
 
=={{header|Perl 6}}==
<lang perl6>my @primes := 2, 3, 5, -> $p {
{{works with|Rakudo Star|2010.09}}
($p+2, $p+4 ... -> $i { $i %% none @primes ... * >= sqrt $i })[*-1]
} ... *;
 
sub prime($i) { (state @)[$i] //= $i %% none @primes ... * >= sqrt $i }
This uses a fairly naive isprime routine. It works but is slow.
 
sub ltp {
<lang perl6>
for (9...1) X (9...1) X (9...1) X (9...1) X (9...1) X (9,7,3) -> $a,$b,$c,$d,$e,$f {
use v6;
my %cache@x := <2[\+] 3$f, 5$e, 7$d, 11$c, 13$b, 17$a 19Z* 23>(1,10,100 >>=>>>... 1*);
return @x[*-1] if prime @x[0] && prime @x[1] && prime @x[2] &&
 
prime @x[3] && prime @x[4] && prime @x[5];
sub isprime ($test) {
}
return %cache{$test} if defined %cache{$test};
return (%cache{$test} = 0) if $test <= 25;
my $r = floor($test ** .5);
return (%cache{$test} = 0) unless $test % $_ for (2, 3, * + 2 ... * >= $r);
return (%cache{$test} = 1);
}
 
sub trunc_primeinfix:<*+> ($typea,$b) { $limita is* copy)10 {+ $b }
 
$limit += ($limit % 2 ?? 0 !! 1);
sub rtp {
for ($limit, * - 2 ... * < 2 ) -> $loop {
for 7,5,3 {
next if $loop ~~ /0/; # No zeros allowed
for grep &prime, ($_ X*+ 9,7,3,1) {
my $this = $loop;
for grep &prime, ($_ whileX*+ $this.&isprime9,7,3,1) {
for grep &prime, ($_ X*+ 9,7,3,1) {
$this.=subst($type, '');
for grep &prime, ($_ X*+ 9,7,3,1) {
return $loop unless $this;
for grep &prime, ($_ X*+ 9,7,3,1) {
}
return $_;
}
}
}
}
}
}
}
 
say "Highest ltp: ", ltp;
say "Largest Left Truncatable Prime < 1000000: ",trunc_prime(rx/^\d/, 1000000);
say "Highest rtp: ", rtp;</lang>
say "Largest Right Truncatable Prime < 1000000: ",trunc_prime(rx/\d$/, 1000000);
{{out}}
</lang>
<pre>Highest ltp: 999983
Output:
Highest rtp: 739399</pre>
<pre>
Largest Left Truncatable Prime < 1000000: 998443
Largest Right Truncatable Prime < 1000000: 739339
</pre>
 
=={{header|PicoLisp}}==
Anonymous user