Skip to content

Instantly share code, notes, and snippets.

@tomasherman
Created January 11, 2012 20:28
Show Gist options
  • Save tomasherman/1596598 to your computer and use it in GitHub Desktop.
Save tomasherman/1596598 to your computer and use it in GitHub Desktop.
sumofdigits
def sumOfDigits(x: BigInt, sum: BigInt):BigInt = { //funkce se menuje sumOfDigits, bere dva argumenty, x kterej ma typ BigInt a sum, taky BigInt a vraci BigInt
x match { //tohle rika ze se ma porovnavat x s nasledujicim:
case xx if xx == 0 => sum //pokud x, ktery prejmenuju na xx == 0, tak to znamena ze sme vsechno secetli a v promenny sum je suma vsech cisel
case xx => sumOfDigits(xx / 10, sum + (xx % 10)) //pokud to neni 0, tak tam jeste nejaky cislice zbyvaj...zavolame znova sumOfDigits, ale s upravenejma parametrama tak, ze z x zahodime posledni cislo a k sume to posledni cislo pricteme
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment