Smallest FizzBuzz program

This is really bugging me. Someone posted a golf challenge to write a smallest FizzBuzz program here:

http://golf.shinh.org/p.rb?FizzBuzz (although the site was down when I checked it a few minutes ago)

Basically, the challenge is to write the smallest Ruby program that will print the numbers from 1 to 100 except:
* substitute Fizz for numbers that are multiples of 3
* substitute Buzz for numbers that are multiples of 5
* substitute FizzBuzz for numbers that are multiples of both 3 and 5

Also see: http://weblog.raganwald.com/2007/01/dont-overthink-fizzbuzz.html

The winning entry is at 56 bytes and I can't get below 65 bytes with the following:

1.upto(100){|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

I think if the substring indices could be computed mathematically instead of logically, it might work, but it's possible an entirely new approach is necessary.

It works correctly, so to see acceptable output, just run it.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...

Can any Ruby guru out there get it down to 56 bytes?

Brian

1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

Shaves off 3 bytes. :slight_smile:

···

On 3/2/07, Brian Adkins <lojicdotcomNOSPAM@gmail.com> wrote:

This is really bugging me. Someone posted a golf challenge to write a
smallest FizzBuzz program here:

anarchy golf - FizzBuzz (although the site was down when I
checked it a few minutes ago)

Basically, the challenge is to write the smallest Ruby program that will
print the numbers from 1 to 100 except:
* substitute Fizz for numbers that are multiples of 3
* substitute Buzz for numbers that are multiples of 5
* substitute FizzBuzz for numbers that are multiples of both 3 and 5

Also see: Don't Overthink FizzBuzz

The winning entry is at 56 bytes and I can't get below 65 bytes with the
following:

1.upto(100){|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

I think if the substring indices could be computed mathematically
instead of logically, it might work, but it's possible an entirely new
approach is necessary.

It works correctly, so to see acceptable output, just run it.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...

Can any Ruby guru out there get it down to 56 bytes?

Brian

--
http://www.jeremymcanally.com/

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:

http://www.rubyinpractice.com/

This is really bugging me. Someone posted a golf challenge to write a
smallest FizzBuzz program here:

anarchy golf - FizzBuzz (although the site was down when I
checked it a few minutes ago)

Basically, the challenge is to write the smallest Ruby program that will
print the numbers from 1 to 100 except:
* substitute Fizz for numbers that are multiples of 3
* substitute Buzz for numbers that are multiples of 5
* substitute FizzBuzz for numbers that are multiples of both 3 and 5

..

Can any Ruby guru out there get it down to 56 bytes?

55 bytes:
1.upto(?d){|i,x|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":i}

Replace ?d with 100 if you want 56 bytes :slight_smile:

···

On Mar 2, 11:06 am, Brian Adkins <lojicdotcomNOS...@gmail.com> wrote:

Brian Adkins wrote:

This is really bugging me. Someone posted a golf challenge to write a smallest FizzBuzz program here:

anarchy golf - FizzBuzz (although the site was down when I checked it a few minutes ago)

I submitted the resulting program to the site (which is back up now) under 'comp.lang.ruby' since multiple people contributed. Comes in at 57 bytes - only 1 byte longer than the best. So apparently the site does count the EOF byte.

1.upto(?d){|i|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":x||i}

http://golf.shinh.org/p.rb?FizzBuzz

Interestingly, the following Ruby program (with a little help from sed that my friend passed along) comes in at exactly 56 bytes, but the test site rightfully failed it due to the attempt to shell the command:

puts `seq 100|sed -e'5~5s/.*/Buzz/;3~3s/^[0-9]*/Fizz/'`

I was trying to submit to program for the prime number challenge, but I
can't make it work because I don't understand how does this website do the
input to the submitted programs.
I mean that when I use ARGV or $*, it seems these variables are empty (just
submitting a test program that does a "pp ARGV" outputs an empty array).

Is there anyone who knows how can I get the input for this website ?

Brian Adkins wrote:

This is really bugging me. Someone posted a golf challenge to write a
smallest FizzBuzz program here:
...

Hey! Stop this! It's supposed to be the dreadfull scripting language
called Pearl that's totally unreadable and it's abusers waste the
precious time trying to beat each other by writing something in as few
characters as possible!

:stuck_out_tongue:

Jenda

···

--
Posted via http://www.ruby-forum.com/\.

1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

Shaves off 3 bytes. :slight_smile:

But it messes with the output a bit...
You can save one byte doing this: 1.upto(?d)

Regards,
Rimantas

···

--
http://rimantas.com/

Jeremy McAnally wrote:

1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

Shaves off 3 bytes. :slight_smile:

Thanks, but that breaks the output by adding "", so it won't do.

100.times{|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

another two. :slight_smile:

  robert

···

On 02.03.2007 17:34, Jeremy McAnally wrote:

1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

Shaves off 3 bytes. :slight_smile:

Ummm..... funny.... I see 9 and 12 in the output when I ran this :slight_smile:

vsv wrote:

···

On Mar 2, 11:06 am, Brian Adkins <lojicdotcomNOS...@gmail.com> wrote:

This is really bugging me. Someone posted a golf challenge to write a
smallest FizzBuzz program here:

anarchy golf - FizzBuzz (although the site was down when I
checked it a few minutes ago)

Basically, the challenge is to write the smallest Ruby program that will
print the numbers from 1 to 100 except:
* substitute Fizz for numbers that are multiples of 3
* substitute Buzz for numbers that are multiples of 5
* substitute FizzBuzz for numbers that are multiples of both 3 and 5

..

Can any Ruby guru out there get it down to 56 bytes?

55 bytes:
1.upto(?d){|i,x|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":i}

Replace ?d with 100 if you want 56 bytes :slight_smile:

Interesting. But it doesn't produce proper output :frowning:

Did you try to read from $stdin? I know that is kind of stupid to ask,
but sometimes we do not see what is hidden in front of our eyes :slight_smile:

Robert

···

On 3/5/07, Guillaume Nargeot <guillaume.nargeotDONOTFUCKINGSPAM@fusionsystems.co.jp> wrote:

I was trying to submit to program for the prime number challenge, but I
can't make it work because I don't understand how does this website do the
input to the submitted programs.
I mean that when I use ARGV or $*, it seems these variables are empty (just
submitting a test program that does a "pp ARGV" outputs an empty array).

Is there anyone who knows how can I get the input for this website ?

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

The email address simply drips irony :slight_smile:

m.

···

On 3/7/07, Jenda Krynicky <jenda@cpan.org> wrote:

Hey! Stop this! It's supposed to be the dreadfull scripting language
called Pearl that's totally unreadable and it's abusers waste the
precious time trying to beat each other by writing something in as few
characters as possible!

Rimantas Liubertas wrote:

1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

Shaves off 3 bytes. :slight_smile:

But it messes with the output a bit...
You can save one byte doing this: 1.upto(?d)

Awesome, ASCII value of letter d ! Only 8 more bytes to shave off :slight_smile:

···

Regards,
Rimantas
--
http://rimantas.com/

?d.times{|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

Hm...

  robert

···

On 02.03.2007 18:00, Rimantas Liubertas wrote:

1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

Shaves off 3 bytes. :slight_smile:

But it messes with the output a bit...
You can save one byte doing this: 1.upto(?d)

Robert Klemme wrote:

1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

Shaves off 3 bytes. :slight_smile:

100.times{|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}

another two. :slight_smile:

Nope. 100.times is not equivalent to 1.upto(100) - off by one error.

···

On 02.03.2007 17:34, Jeremy McAnally wrote:

    robert

1.upto(?d){|i,x| i%3<1&&x=:Fizz;puts (i%3<1||i%5<1)?"#{x}Buzz":i} is
back up to 65. Shame.

it is my fault, last second optimization is always wrong
(my lunch is too short and I can't use ruby in the office :frowning: ),
best I can get if 58 chars:

1.upto(?d){|i,x|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":x||i}

hope it works OK :slight_smile:

···

On Mar 2, 2:21 pm, "Kyle Schmitt" <kyleaschm...@gmail.com> wrote:

Ummm..... funny.... I see 9 and 12 in the output when I ran this :slight_smile:

And what's pearl? I thought it was perl. What does it mean if one
doesn't know how to properly spell one's favorite language?

You could always tell who was really in the Smalltalk community or at
least who wasn't when they spelled it SmallTalk.

···

On 3/7/07, Martin DeMello <martindemello@gmail.com> wrote:

On 3/7/07, Jenda Krynicky <jenda@cpan.org> wrote:
>
> Hey! Stop this! It's supposed to be the dreadfull scripting language
> called Pearl that's totally unreadable and it's abusers waste the
> precious time trying to beat each other by writing something in as few
> characters as possible!

The email address simply drips irony :slight_smile:

--
Rick DeNatale

This approach does 0..99 though.... :frowning:
-Mat

···

On Mar 2, 2007, at 12:15 PM, Robert Klemme wrote:

?d.times{|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}