Project Euler

Project Euler Problem 34

steloflute 2012. 6. 9. 00:05

Problem 34

03 January 2003

145 is a curious 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.


Answer:
40730

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.IO;

namespace Euler {
    class Program {
        static void Main(string[] args) {
            var factorial = new int[10];
            factorial[0] = 1;
            foreach (var i in Enumerable.Range(1, 9)) {
                factorial[i] = factorial[i - 1] * i;
            }

            Console.WriteLine(Enumerable.Range(10, 1000000)
                .Where(x => x.ToString().Select(c => factorial[c - '0']).Sum() == x)
                .Sum());
        }
    }
}



'Project Euler' 카테고리의 다른 글

Project Euler Problem 36  (0) 2012.06.09
Project Euler Problem 35  (0) 2012.06.09
Project Euler Problem 33  (0) 2012.06.09
Project Euler Problem 32  (0) 2012.06.09
Project Euler Problem 31  (0) 2012.06.09