This commit is contained in:
Dr. Matthias Ratajczak
2022-09-16 16:03:44 +02:00
parent e3c5151a82
commit 51b0da331e
2 changed files with 50 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "euler56"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rug = "1.17.0"
+41
View File
@@ -0,0 +1,41 @@
use rug::{ops::Pow, Integer};
fn main() {
let mut solution = vec![];
for a in 1..100 {
for b in 1..100 {
let aa = Integer::from(a);
let result = digital_sum(aa.pow(b));
println!("{result}");
solution.push(result);
}
}
let max = solution.into_iter().max().unwrap();
println!("{}", max);
}
fn digital_sum(int: Integer) -> u32 {
int.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap() as u32)
.sum()
}
#[cfg(test)]
mod test {
use super::digital_sum;
use rug::ops::Pow;
use rug::Integer;
#[test]
fn test_digital_sum() {
let result = digital_sum(Integer::from(100).pow(100));
assert_eq!(result, 1);
let result = digital_sum(Integer::from(5).pow(2));
assert_eq!(result, 7);
let result = digital_sum(Integer::from(6).pow(2));
assert_eq!(result, 9);
}
}