Skip to content

Instantly share code, notes, and snippets.

@rayning0
Created September 26, 2013 03:44
Show Gist options
  • Save rayning0/6709646 to your computer and use it in GitHub Desktop.
Save rayning0/6709646 to your computer and use it in GitHub Desktop.
Fizzbuzz
# Raymond Gan
def fizzbuzz(n)
return "FizzBuzz" if (n % 3 == 0) && (n % 5 == 0)
return "Fizz" if n % 3 == 0
return "Buzz" if n % 5 == 0
end
fizz, buzz, fizzbuzz = [], [], []
(1..50).each do |n|
case fizzbuzz(n)
when "Fizz"
fizz << n
when "Buzz"
buzz << n
when "FizzBuzz"
fizzbuzz << n
end
end
puts "Fizz numbers (divisible by 3 only) = #{fizz}"
puts "Buzz numbers (divisible by 5 only) = #{buzz}"
puts "FizzBuzz numbers (divisible by 3 and 5) = #{fizzbuzz}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment