Rust Implementation of Problem 20
View source code here on GitHub!
Problem Solution
- pub fn problems::p0020::p0020() -> utils::Answer
1/*
2Project Euler Problem 20
3
4Problem:
5
6n! means n × (n − 1) × ... × 3 × 2 × 1
7
8For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
9and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
10
11Find the sum of the digits in the number 100!
12*/
13use crate::include::utils::Answer;
14
15pub fn p0020() -> Answer {
16 let mut numbers: Vec<u128> = vec![0; 5];
17 let ten36: u128 = 1000000000000000000000000000000000000;
18 numbers[0] = 1;
19 for i in 2..=100 {
20 for number in numbers.iter_mut() {
21 *number *= i;
22 }
23 for j in 0..4 {
24 if numbers[j] > ten36 {
25 numbers[j + 1] += numbers[j] / ten36;
26 numbers[j] %= ten36;
27 }
28 }
29 }
30 let mut answer: i128 = 0;
31 let mut power: u128 = 1;
32 for _ in 0..36 {
33 for number in numbers.iter() {
34 answer += ((number / power) % 10) as i128;
35 }
36 power *= 10;
37 }
38 return Answer::Int(answer);
39}