C# Implementation of Problem 34

View source code here on GitHub!

Includes

Problem Solution

class p0034
: Euler.IEuler
object 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*/
17using System;
18
19namespace Euler
20{
21    public class p0034 : IEuler
22    {
23        public object Answer()
24        {
25            uint answer = 0;
26            for (uint x = 10; x < 100000; x += 1)
27            {
28                string xs = x.ToString();
29                uint sum = 0;
30                for (byte i = 0; i < xs.Length; i += 1)
31                    sum += (uint)Mathematics.Factorial((ulong)(xs[i] - '0'));
32                if (sum == x)
33                    answer += x;
34            }
35            return (ushort)answer;
36        }
37    }
38}

Tags: factorial, digit-sum