Skip to content

Instantly share code, notes, and snippets.

@wteuber
Created February 5, 2013 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wteuber/4713375 to your computer and use it in GitHub Desktop.
Save wteuber/4713375 to your computer and use it in GitHub Desktop.
Calculates the digit sum of an integer number
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