Skip to content

Instantly share code, notes, and snippets.

@mattvanhorn
Created September 5, 2014 13:52
Show Gist options
  • Save mattvanhorn/153f0e7c11307ceb957f to your computer and use it in GitHub Desktop.
Save mattvanhorn/153f0e7c11307ceb957f to your computer and use it in GitHub Desktop.
FizzBuzz without modulo or division
class FizzBuzz
def run
(1..100).each do |number|
result = ''
if divisible_by_3?(number)
result << "Fizz"
end
if divisible_by_5?(number)
result << "Buzz"
end
if result == ''
result = number.to_s
end
puts result
end
end
def divisible_by_3?(number)
digits = number.to_s.split('')
while digits.length > 1 do
digits = digits.map(&:to_i).reduce(:+).to_s.split('')
end
[3,6,9].include?(digits.first.to_i)
end
def divisible_by_5?(number)
last_digit = number.to_s[-1].to_i
[0,5].include?(last_digit)
end
end
FizzBuzz.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment