Skip to content

Instantly share code, notes, and snippets.

@levinotik
Created July 4, 2012 03:52
Show Gist options
  • Save levinotik/3045176 to your computer and use it in GitHub Desktop.
Save levinotik/3045176 to your computer and use it in GitHub Desktop.
def sumOfDigits(n: Int) = {
@annotation.tailrec def sumOfDigitsAcc(n: Int, acc: Int):Int = {
n match {
case 0 => n + acc
case _ => sumOfDigitsAcc(n/10, acc + (n%10))
}
}
sumOfDigitsAcc(n, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment