RosettaCodeData/Task/Modular-exponentiation/Tcl/modular-exponentiation-3.tcl

11 lines
194 B
Tcl
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
package require Tcl 8.5
proc modexp {a b n} {
for {set c 1} {$b} {set a [expr {$a*$a % $n}]} {
if {$b & 1} {
set c [expr {$c*$a % $n}]
}
set b [expr {$b >> 1}]
}
return $c
}