home
>
Project Euler
>
ForNext
Shut the fuck up and write some code
Problem 20
100! の各桁の数字の和を求めよ
を と表す.
例えば, となる.
この数の各桁の合計は である.
では, の各桁の数字の和を求めよ.
Factorial digit sum
means
For example, ,
and the sum of the digits in the number .
Find the sum of the digits in the number
VBScript
JScript
Perl
PHP
Python
Ruby
PowerShell
Scala
更新日 : 2013.02.02
scala> List.range(1, 100).product res0: Int = 0
scala> List.range(BigInt(1), BigInt(100)).product <console>:6: error: type mismatch; found : scala.math.BigInt required: Int List.range(BigInt(1), BigInt(100)).product ^
scala> List.range(BigInt(1), BigInt(100)).reduceLeft(_*_) <console>:6: error: type mismatch; found : scala.math.BigInt required: Int List.range(BigInt(1), BigInt(100)).reduceLeft(_*_) ^
scala> List.range(1, 100).map(BigInt(_)).product res3: scala.math.BigInt = 933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000
scala> List.range(1, 100).map(BigInt(_)).product.toString.toCharArray res4: Array[Char] = Array(9, 3, 3, 2, 6, 2, 1, 5, 4, 4, 3, 9, 4, 4, 1, 5, 2, 6, 8, 1, 6, 9, 9, 2, 3, 8, 8, 5, 6, 2, 6, 6, 7, 0, 0, 4, 9, 0, 7, 1, 5, 9, 6, 8, 2, 6, 4, 3, 8, 1, 6, 2, 1, 4, 6, 8, 5, 9, 2, 9, 6, 3, 8, 9, 5, 2, 1, 7, 5, 9, 9, 9, 9, 3, 2, 2, 9, 9, 1, 5, 6, 0, 8, 9, 4, 1, 4, 6, 3, 9, 7, 6, 1, 5, 6, 5, 1, 8, 2, 8, 6, 2, 5, 3, 6, 9, 7, 9, 2, 0, 8, 2, 7, 2, 2, 3, 7, 5, 8, 2, 5, 1, 1, 8, 5, 2, 1, 0, 9, 1, 6, 8, 6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> List.range(1, 100).map(BigInt(_)).product.toString.toCharArray.map(_.toInt - 48).sum res5: Int = 648
F#
更新日 : 2013.02.02
> [1I..100I] |> List.reduce(*) - ;; val it : System.Numerics.BigInteger = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 {IsEven = true; IsOne = false; IsPowerOfTwo = false; IsZero = false; Sign = 1;}
> [1I..100I] - |> List.reduce(*) - |> System.Convert.ToString - ;; val it : string = "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"
> ([1I..100I] - |> List.reduce(*) - |> System.Convert.ToString).ToCharArray() - ;; val it : char [] = [|'9'; '3'; '3'; '2'; '6'; '2'; '1'; '5'; '4'; '4'; '3'; '9'; '4'; '4'; '1'; '5'; '2'; '6'; '8'; '1'; '6'; '9'; '9'; '2'; '3'; '8'; '8'; '5'; '6'; '2'; '6'; '6'; '7'; '0'; '0'; '4'; '9'; '0'; '7'; '1'; '5'; '9'; '6'; '8'; '2'; '6'; '4'; '3'; '8'; '1'; '6'; '2'; '1'; '4'; '6'; '8'; '5'; '9'; '2'; '9'; '6'; '3'; '8'; '9'; '5'; '2'; '1'; '7'; '5'; '9'; '9'; '9'; '9'; '3'; '2'; '2'; '9'; '9'; '1'; '5'; '6'; '0'; '8'; '9'; '4'; '1'; '4'; '6'; '3'; '9'; '7'; '6'; '1'; '5'; '6'; '5'; '1'; '8'; '2'; '8'; ...|]
> ([1I..100I] - |> List.reduce(*) - |> System.Convert.ToString).ToCharArray() - |> Array.map(int) - |> Array.map(fun n -> n - 48) - |> Array.sum - ;; val it : int = 648