Coin de Gamma
11 months ago
1 changed files with 26 additions and 0 deletions
@ -0,0 +1,26 @@ |
|||||||
|
// https://rob.co.bb/posts/2019-02-10-modular-exponentiation-in-rust/
|
||||||
|
fn mod_pow(mut base: u128, mut exp: u128, modulus: u128) -> u128 { |
||||||
|
if modulus == 1 { return 0 } |
||||||
|
let mut result = 1; |
||||||
|
base = base % modulus; |
||||||
|
while exp > 0 { |
||||||
|
if exp % 2 == 1 { |
||||||
|
result = result * base % modulus; |
||||||
|
} |
||||||
|
exp = exp >> 1; |
||||||
|
base = base * base % modulus |
||||||
|
} |
||||||
|
result |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
fn main() { |
||||||
|
let m = 10000000000; |
||||||
|
let mut res = 0u128; |
||||||
|
for n in 1..1000 { |
||||||
|
let t = mod_pow(n, n, m); |
||||||
|
res += t; |
||||||
|
res = res % m; |
||||||
|
} |
||||||
|
println!("{}", res); |
||||||
|
} |
Loading…
Reference in new issue