Coin de Gamma 1 year ago
parent
commit
6abbd887a6
  1. 26
      048/main.rs

26
048/main.rs

@ -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…
Cancel
Save