Lucas-Carmichael numbers: Difference between revisions

→‎{{header|Raku}}: better performance (~3x faster)
m (→‎{{header|Python}}: use the built-in `pow(b,-1,m)` for computing the modular inverse (faster))
(→‎{{header|Raku}}: better performance (~3x faster))
 
Line 588:
<syntaxhighlight lang="raku" line># 20231224 Raku programming solution
 
sub primorial($n) { [*] @primes((2..*).grep: *.is-prime)[^$n] }
use Math::Root;
sub divceil ($x, $y) { ($x %% $y ?? 0 !! 1) + $x div $y } # ceil(x/y)
use Math::Primesieve;
 
my @primes = Math::Primesieve.primes(100_000); # Primorial_numbers#Raku
sub primorial($n) { [*] @primes[^$n] }
 
sub divceil ($x, $y) { ($x %% $y ?? 0 !! 1) + $x div $y } # ceil(x/y)
 
sub invmod($n, $modulo) { # rosettacode.org/wiki/Modular_inverse#Raku
my ($c, $d, $uc, $vc, $ud, $vd, $q) = ($n % $modulo, $modulo, 1, 0, 0, 1);
while $c != 0 {
($q, $c, $d) = ($d div $c, $d % $c, $c);
($uc, $vc, $ud, $vd) = ($ud - $q×$uc, $vd - $q×$vc, $uc, $vc);
}
return $ud % $modulo;
}
 
sub LC_in_range ($A is copy, $B, $k) {
 
my ($max_p, @LC) = Int(sqrt($B + 1) - 1);
$A = max(primorial($k + 1) +> 1, $A);
 
sub SUB($m, $L, $lo is copy, $k) {
my $hi = ( $k == 1 ) ?? ($B div $m) !! iroot ($B div $m), $k;
return if $lo > $hi;
 
if $k == 1 {
 
my $hi = min($max_pB ifdiv $hi >m, $max_p);
$lo = max($lo, divceil($A, $m));
return if $lo > $hi;
 
my $t = $L - invmodexpmod($m, -1, $L);
$t += $L while $t < $lo;
return if $udt %> $modulohi;
 
for $t, $t+$L ... $hi -> $p {
Line 632 ⟶ 615:
return;
}
 
for Math::Primesieve$lo .primes. Int(($lo,B div $m)**(1/$hik)) -> $p {
if $p.is-prime and ($m gcd ($p + 1)) == 1 {
SUB($m * $p, ($L lcm ($p + 1)), $p + 1, $k - 1)
}
}
};
 
SUB(1, 1, 3, $k);
2,756

edits