Created
February 5, 2013 09:44
-
-
Save wteuber/4713375 to your computer and use it in GitHub Desktop.
Calculates the digit sum of an integer number
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
class Integer | |
def digit_sum(base = 10) | |
result = 0 | |
number = self | |
while(number > 0) do | |
result += number % base | |
number /= base | |
end | |
result | |
end | |
end | |
(2**906).digit_sum #1234 | |
1234.digit_sum #10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment