Created
January 15, 2016 12:36
-
-
Save shkesar/d5f2c1e9f3d0d70c49da to your computer and use it in GitHub Desktop.
Project Euler Problem 20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main extends App { | |
def factorial(n: BigInt, acc: BigInt = 1): BigInt = | |
if (n == 0) acc else factorial(n - 1, acc * n) | |
def sumOfDigits(a: BigInt) = { | |
var n = a | |
val digits = collection.mutable.ArrayBuffer[Int]() | |
while (n > 0) { | |
digits += (n % 10).toInt | |
n = n / 10 | |
} | |
digits.sum | |
} | |
val fact = factorial(BigInt(100)) | |
val sum = sumOfDigits(fact) | |
println(fact + " " + sum) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment