diff --git a/.gitignore b/.gitignore index 088ba6b..d6d7fb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,10 @@ # Generated by Cargo +# Ignore all +/src/bin/* + +# Unignore all with extensions +!/src/bin/*.* + # will have compiled files and executables /target/ diff --git a/src/bin/34.rs b/src/bin/34.rs new file mode 100644 index 0000000..b64932d --- /dev/null +++ b/src/bin/34.rs @@ -0,0 +1,46 @@ +//145 is an interesting number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. +//Find the sum of all numbers which are equal to the sum of the factorial of their digits. +//Note: as 1! = 1 and 2! = 2 are not sums they are not included. + +#[warn(unused_imports)] +fn factorial_function(num: i32) -> i32 { + match num { + 0 => 1, + _ => num * factorial_function(num - 1) + } +} + +fn compute_sum(a: i32, b: i32) -> i32 { + let mut total = 0; + for i in a..b { + let mut sum = 0; + let x = i.to_string(); + for y in x.chars(){ + let z = (y.to_string()).parse::().unwrap(); + sum += factorial_function(z); + } + + if i == sum { + total +=sum; + } + } + return total; +} + +fn main() { + let start: i32 = 0; + let end: i32 = 1_000_000; + let result = compute_sum(start, end); + println!("Sum of all numbers : {}", result); +} + +#[test] +fn test_factorial() { + assert_eq!(factorial_function(3), 6); +} + +#[test] +fn test_compute_sum(){ + assert_eq!(compute_sum(3, 1000), 145); +} +