Created
July 23, 2013 16:48
-
-
Save oholiab/6064008 to your computer and use it in GitHub Desktop.
Recursively adding the digits of a multiple of 3 until there is only one digit left and it is one of 3, 6 or 9 shows that it is a multiple of 3 as shown in this script that compares it to the modulo method
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
#!/usr/bin/ruby | |
i=0 | |
while(i<300) | |
puts "mod: #{i}" if i % 3 == 0 | |
string = i.to_s | |
while string.length > 1 | |
total = 0 | |
string.split('').each {|x| total += x.to_i} | |
string = total.to_s | |
end | |
puts "add: #{i}" if [3,6,9].include? total | |
i+=1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment