Problem 34
- 投稿者 : rei
Original
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.
和訳
145は面白い数である. 1! + 4! + 5! = 1 + 24 + 120 = 145となる.
各桁の数の階乗の和が自分自身と一致するような数の総和を求めよ.
注: 1! = 1 と 2! = 2 は総和に含めてはならない
当てにならないソースコード(C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using System.Collections.Generic; namespace ProjectEuler { class Problem34 : Problem{ public Problem34() { long answer = 0; List<long> fact = new List<long>(); for (long i = 0; i < 10; i++) { fact.Add(Factorial(i)); } long limit = 9; for (int i = 1; fact[9] * i > limit; i++, limit = fact[9] * (i+1)); for (long i = 3; i < limit; i++) { List<int> digitList = Numerics.MakeDigits(i); long sum = 0; foreach (var digit in digitList) { sum += fact[(int)digit]; } if (i == sum) { answer += sum; //Console.WriteLine(i); } } Console.WriteLine("> " + answer); } long Factorial(long x){ return x > 1 ? x * Factorial(x - 1) : 1; } } } |

rei@sikios.com
コメントはまだありません。