Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ version = "0.1.0"
authors = ["ghost <mattgathu@gmail.com>"]

[dependencies]
num-bigint = "0.2.0"
num-traits = "0.2.5"
38 changes: 38 additions & 0 deletions src/bin/20.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// n! means n × (n − 1) × ... × 3 × 2 × 1
//
// For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
// and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
//
// Find the sum of the digits in the number 100!

extern crate num_bigint;
extern crate num_traits;

use num_bigint::BigUint;


/// Computes the Factorial the BigUint type
fn factorial(num: u32) -> BigUint {
(1..=num).map(BigUint::from).product()
}

fn problem_20_sol_1(limit: u32) -> u32 {
let sum_str = format!("{}", factorial(limit));
sum_str.chars().map(|c| c.to_digit(10).unwrap()).sum()
}

fn main() {
let limit = 100;
println!("Sum of the digits in the number {}!: {}",
limit, problem_20_sol_1(100));
}

#[test]
fn test_factorial() {
assert_eq!(BigUint::from_u32(3628800).unwrap(), factorial(10));
}

#[test]
fn test_problem_20_sol_1() {
assert_eq!(27, problem_20_sol_1(10));
}