Problem 20
- 投稿者 : rei
Original
n! means n × (n – 1) × … × 3 × 2 × 1
Find the sum of the digits in the number 100!
和訳
n × (n – 1) × … × 3 × 2 × 1 を n! と表す。
100! の各桁の数字の合計を求めよ。
当てにならないソースコード(C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System; namespace ProjectEuler { class Problem20 { public Problem20() { Console.WriteLine(this.ToString()); int answer = 0; BigInt number = new BigInt(1); for (int i = 2; i <= 100; i++) { number.Multiple(i); } foreach (var c in number.ToDec()) { answer += int.Parse(c.ToString()); } Console.WriteLine("> " + answer); } } } |
BigInt
これは多倍長整数クラスを作る問題ですね。
当てにならないソースコード(F#)
1 2 3 4 5 6 7 8 9 10 11 | let problem20 () = let rec fact n = match n = 1I with | true -> 1I | _ -> n * fact (n-1I) let rec sum n = match n = 0I with | true -> 0I | _ -> n % 10I + sum (n / 10I) 100I |> fact |> sum |> int |> (printfn "Problem 20 > %d") |

