Skip to content

Instantly share code, notes, and snippets.

@oholiab
Created July 23, 2013 16:48
Show Gist options
  • Save oholiab/6064008 to your computer and use it in GitHub Desktop.
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
#!/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