You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
598 B
27 lines
598 B
1 year ago
|
// 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);
|
||
|
}
|