From 6abbd887a698629d0f1ea40c404fc6ee911c4e74 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sun, 17 Dec 2023 14:15:46 +0000 Subject: [PATCH] 048 --- 048/main.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 048/main.rs diff --git a/048/main.rs b/048/main.rs new file mode 100644 index 0000000..5d34074 --- /dev/null +++ b/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); +}