use num_bigint::BigUint;
fn sum_of_digits(n: u32) -> u32 {
// Compute 2^n using BigUint
let two = BigUint::from(2u32);
let power = two.pow(n);
// Convert to string and sum digits
power
.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap())
.sum()
}
fn main() {
for &n in &[15, 100, 1000] {
println!("Sum of digits of 2^{} is: {}", n, sum_of_digits(n));
}
}
/*
run:
Sum of digits of 2^15 is: 26
Sum of digits of 2^100 is: 115
Sum of digits of 2^1000 is: 1366
*/