Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
9
Task/Modular-inverse/Perl/modular-inverse-1.pl
Normal file
9
Task/Modular-inverse/Perl/modular-inverse-1.pl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use bigint; say 42->bmodinv(2017);
|
||||
# or
|
||||
use Math::ModInt qw/mod/; say mod(42, 2017)->inverse->residue;
|
||||
# or
|
||||
use Math::Pari qw/PARI lift/; say lift PARI "Mod(1/42,2017)";
|
||||
# or
|
||||
use Math::GMP qw/:constant/; say 42->bmodinv(2017);
|
||||
# or
|
||||
use ntheory qw/invmod/; say invmod(42, 2017);
|
||||
15
Task/Modular-inverse/Perl/modular-inverse-2.pl
Normal file
15
Task/Modular-inverse/Perl/modular-inverse-2.pl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
sub invmod {
|
||||
my($a,$n) = @_;
|
||||
my($t,$nt,$r,$nr) = (0, 1, $n, $a % $n);
|
||||
while ($nr != 0) {
|
||||
# Use this instead of int($r/$nr) to get exact unsigned integer answers
|
||||
my $quot = int( ($r - ($r % $nr)) / $nr );
|
||||
($nt,$t) = ($t-$quot*$nt,$nt);
|
||||
($nr,$r) = ($r-$quot*$nr,$nr);
|
||||
}
|
||||
return if $r > 1;
|
||||
$t += $n if $t < 0;
|
||||
$t;
|
||||
}
|
||||
|
||||
say invmod(42,2017);
|
||||
Loading…
Add table
Add a link
Reference in a new issue