Skip to content

Instantly share code, notes, and snippets.

@andrewberls
Created February 3, 2013 01:52
Show Gist options
  • Save andrewberls/4700216 to your computer and use it in GitHub Desktop.
Save andrewberls/4700216 to your computer and use it in GitHub Desktop.
The classic FizzBuzz challenge, with an infusion of code golf
# Print the numbers from 1 to 100, except for multiples of 3 print "Fizz", for multiples of 5 print "Buzz",
# and for multiples of 3 and 5 print "FizzBuzz".
# Ex:
# 1
# 2
# Fizz
# 4
# Buzz
# Fizz
# 7
# 8
# etc...
# The challenge is to do it in as few characters as possible. Happy golfing!
# Attempt 1 - 78 chars
1.upto(100){|i|j=i%3==0;k=i%5==0;puts j&&k ?"FizzBuzz":j ?"Fizz":k ?"Buzz":i}
# Attempt 2 (With some inspiration from online) - 67 chars
1.upto(100){|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment