A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
17
Task/Modular-inverse/Tcl/modular-inverse-1.tcl
Normal file
17
Task/Modular-inverse/Tcl/modular-inverse-1.tcl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
proc gcdExt {a b} {
|
||||
if {$b == 0} {
|
||||
return [list 1 0 $a]
|
||||
}
|
||||
set q [expr {$a / $b}]
|
||||
set r [expr {$a % $b}]
|
||||
lassign [gcdExt $b $r] s t g
|
||||
return [list $t [expr {$s - $q*$t}] $g]
|
||||
}
|
||||
proc modInv {a m} {
|
||||
lassign [gcdExt $a $m] i -> g
|
||||
if {$g != 1} {
|
||||
return -code error "no inverse exists of $a %! $m"
|
||||
}
|
||||
while {$i < 0} {incr i $m}
|
||||
return $i
|
||||
}
|
||||
4
Task/Modular-inverse/Tcl/modular-inverse-2.tcl
Normal file
4
Task/Modular-inverse/Tcl/modular-inverse-2.tcl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
puts "42 %! 2017 = [modInv 42 2017]"
|
||||
catch {
|
||||
puts "2 %! 4 = [modInv 2 4]"
|
||||
} msg; puts $msg
|
||||
Loading…
Add table
Add a link
Reference in a new issue