From e058da0bd4cce4a39dee87c4ce078dcd57a2d1c3 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Wed, 4 Dec 2024 10:41:04 +0000 Subject: [PATCH] solutions: 20 --- src/bin/factorial_digit_sum.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/bin/factorial_digit_sum.rs diff --git a/src/bin/factorial_digit_sum.rs b/src/bin/factorial_digit_sum.rs new file mode 100644 index 0000000..a21f52c --- /dev/null +++ b/src/bin/factorial_digit_sum.rs @@ -0,0 +1,19 @@ +use num_bigint::BigUint; + +fn factorial(x: u64) -> BigUint { + let mut product = BigUint::from(1 as u64); + for i in 2..=x { + product *= BigUint::from(i); + } + product.into() +} + +fn main() { + let number = factorial(100); + let mut digits: Vec = vec![]; + for i in number.to_string().chars().filter_map(|c| c.to_digit(10)) { + digits.push(i as u64); + } + + println!("{}", digits.iter().sum::()); +}