Rust Implementation of Problem 34

View source code here on GitHub!

Includes

Problem Solution

pub fn problems::p0034::p0034() -> utils::Answer
 1/*
 2Project Euler Problem 34
 3
 4This ended up being a filtering problem. The problem with my solution is that I
 5am not satisfied with my filter at all. I feel like there is a more efficient
 6way to go about it.
 7
 8Problem:
 9
10145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
11
12Find the sum of all numbers which are equal to the sum of the factorial of
13their digits.
14
15Note: as 1! = 1 and 2! = 2 are not sums they are not included.
16*/
17use crate::include::math::factorial;
18use crate::include::utils::Answer;
19
20pub fn p0034() -> Answer {
21    let mut answer: u32 = 0;
22    for x in 10..100000 {
23        let sum = x.to_string()
24                   .bytes()
25                   .fold(0, |a, b| a + factorial::<u32>(b - b'0'));
26        if sum == x {
27            answer += x;
28        }
29    }
30    return Answer::Int(answer.into());
31}

Tags: factorial, digit-sum