Cipolla's algorithm: Difference between revisions
m
→{{header|Perl 6}}: Minor code simplifications.
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add Perl 6) |
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Minor code simplifications.) |
||
Line 359:
<lang perl6># Legendre operator (𝑛│𝑝)
sub infix:<│> (Int \𝑛, Int \𝑝 where
given 𝑛.expmod( (𝑝-1) div 2, 𝑝 ) {
when 0 { 0 }
Line 376:
note "Invalid parameters ({𝑛}, {𝑝})"
and return Nil if (𝑛│𝑝) != 1;
my
loop
last if ($ω2 = ($a² - 𝑛) % 𝑝)│𝑝 < 0;
}
# define a local multiply operator for Field coordinates
multi sub infix:<*> ( Fp $a, Fp $b ){
Fp.new: :x(($a.x * $b.x + $a.y * $b.y * $ω2) % 𝑝),
:
}
my $r = Fp.new
my $s = Fp.new
for (𝑝+1) +> 1, * +> 1 ...
$r *= $s if $_ % 2;
$s *= $s;
Line 416 ⟶ 414:
for @tests -> ($n, $p) {
my $r = cipolla($n, $p);
say $r ?? "Roots of $n are ($r, {$p-$r}) mod $p"
}
▲}</lang>
{{out}}
<pre>Roots of 10 are (6, 7) mod 13
|