Skip to content

Instantly share code, notes, and snippets.

@shkesar
Created January 15, 2016 12:36
Show Gist options
  • Save shkesar/d5f2c1e9f3d0d70c49da to your computer and use it in GitHub Desktop.
Save shkesar/d5f2c1e9f3d0d70c49da to your computer and use it in GitHub Desktop.
Project Euler Problem 20
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