Created
January 11, 2012 20:28
-
-
Save tomasherman/1596598 to your computer and use it in GitHub Desktop.
sumofdigits
This file contains 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
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