Welcome Guest ( Log In | Register )

Bump TopicReply to this topicRSS feed Start new topic Start Poll

Outline · [ Standard ] · Linear+

> Fizz Buzz, any languages

bluefox
post May 17 2007, 12:18 PM, updated 11y ago

Getting Started
**
Group: Junior Member
Posts: 91

Joined: May 2007
Any one with any good idea how to write an elegant way of fizz buzz? mind sharing?
http://golf.shinh.org/p.rb?FizzBuzz
fyire
post May 17 2007, 12:24 PM

Look at all my stars!!
Group Icon
Group: VIP
Posts: 9,270

Joined: Jan 2003
From: Somewhere out there
OMG! there's actually lunatics out there who went and wrote the thing in Whitespace and Brainf***??
godhpf
post May 17 2007, 12:54 PM

On my way
****
Group: Senior Member
Posts: 544

Joined: Jan 2003
QUOTE(fyire @ May 17 2007, 12:24 PM)
OMG! there's actually lunatics out there who went and wrote the thing in Whitespace and Brainf***??
*
sweat.gif sweat.gif tab space LF repeat N times sweat.gif some ppl really very free

if view with wordpad...entire thing blank sweat.gif

This post has been edited by godhpf: May 17 2007, 12:57 PM
wKkaY
post May 17 2007, 02:17 PM

misutā supākoru
Group Icon
Group: VIP
Posts: 5,884

Joined: Jan 2003
Oh man, this thing took a good part of my afternoon away T_T I made it to 152 chars in C, using two table lookups (one for the words, and another to select which word to use).
nyem
post May 18 2007, 09:30 AM

Enthusiast
*****
Group: Senior Member
Posts: 705

Joined: Jan 2007


FizzBuzz in javascript
CODE
<html>
<body>
    <script>

    for (var i=1;i<=100;i++)     {
         document.write(
              ((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+'<br>'
         )
    }

    </script>
</body>
</html>


seraphiel
post May 18 2007, 09:55 AM

New Member
*
Group: Junior Member
Posts: 31

Joined: Jul 2006
From: Petaling Jaya, Malaysia


Well, since you specifically said interesting here's mine:

CODE
<?foreach(range(1,100)as$i)echo$i%15?$i%5?$i%3?$i:"Fizz":"Buzz":"FizzBuzz","\n";?>


I actually posted about it here (and included a couple of solutions my colleagues came up with) whistling.gif

This post has been edited by seraphiel: May 18 2007, 10:54 AM
zybler
post May 18 2007, 08:23 PM

Casual
***
Group: Junior Member
Posts: 404

Joined: Jan 2003


QUOTE(seraphiel @ May 18 2007, 09:55 AM)
Well, since you specifically said interesting here's mine:

CODE
<?foreach(range(1,100)as$i)echo$i%15?$i%5?$i%3?$i:"Fizz":"Buzz":"FizzBuzz","\n";?>


I actually posted about it here (and included a couple of solutions my colleagues came up with) whistling.gif
*
Nice try, here's 67 chars one smile.gif :

CODE
<?while($i++<100)echo$i%3?$i%5?$i:'':'Fizz',$i%5?'':'Buzz',"<br>"?>




This post has been edited by zybler: May 18 2007, 08:26 PM
seraphiel
post May 18 2007, 09:33 PM

New Member
*
Group: Junior Member
Posts: 31

Joined: Jul 2006
From: Petaling Jaya, Malaysia


I've been coding with error_reporting(E_ALL) too long to even attempt that, zybler tongue.gif

I would say that your solution is definitely not what I'd accept as the shortest simply because it throws a warning whistling.gif
zybler
post May 18 2007, 09:51 PM

Casual
***
Group: Junior Member
Posts: 404

Joined: Jan 2003


QUOTE(seraphiel @ May 18 2007, 09:33 PM)
I've been coding with error_reporting(E_ALL) too long to even attempt that, zybler tongue.gif

I would say that your solution is definitely not what I'd accept as the shortest simply because it throws a warning whistling.gif
*
If you so insists on using it on error_reporting(E_ALL) mode, then here it is:

CODE
<?$i=0;while($i++<100)echo$i%3?$i%5?$i:'':'Fizz',$i%5?'':'Buzz',"\n"?>


Still shorter than yours by 12 bytes, laugh.gif.
seraphiel
post May 18 2007, 10:50 PM

New Member
*
Group: Junior Member
Posts: 31

Joined: Jul 2006
From: Petaling Jaya, Malaysia


Well done tongue.gif
zybler
post May 18 2007, 11:08 PM

Casual
***
Group: Junior Member
Posts: 404

Joined: Jan 2003


Tried it in C. With suggestion by a close friend, managed to push it down to 109 chars.

CODE
main(){char n[4];int i;for(;i++<100;){sprintf(n,"%d",i);printf("%s%s\n",i%3?i%5?n:"":"Fizz",i%5?"":"Buzz");}}


Kinda feel like cheating: no #include<stdio.h> no return type for main and no return in main. The strange thing though, it runs!

This post has been edited by zybler: May 18 2007, 11:12 PM
sunsuron
post May 19 2007, 01:38 AM

olumis.com
******
Group: Senior Member
Posts: 1,320

Joined: Nov 2004
From: olumis.com



I guess, if your main intention was to get the shortest, then it is not cheating. I have a few C collections not long time ago, exam preparation for a job interview just in case. Hehe. biggrin.gif

CODE

main(){char i=0,n[3];while(i++<100)printf("%s%s%s\n",i%3?"":"Fizz",i%5?"":"Buzz",(i%3&&i%5&&sprintf(n,"%d",i))?n:"");}


CODE

main(){int i=0;while(i++<100)(i%3||!printf("Fizz"))*(i%5||!printf("Buzz"))&&printf("%d",i),printf("\n");}


CODE

#include <stdio.h>
static const char *t[] = {"%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n"};
int main()
{
 unsigned int i;
 for(i = 1; i <= 100; i++) printf(t[3&19142723>>2*i%30], i);
 return 0;
}

nyem
post May 19 2007, 01:48 AM

Enthusiast
*****
Group: Senior Member
Posts: 705

Joined: Jan 2007


QUOTE(nyem @ May 18 2007, 09:30 AM)
FizzBuzz in javascript
CODE
<html>
<body>
    <script>

    for (var i=1;i<=100;i++)     {
         document.write(
              ((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+'<br>'
         )
    }

    </script>
</body>
</html>

*
just realised that I can execute the javascript by pasting it into the browser address bar, no need to embed in html.

CODE
javascript:for(var i=1;i<=100;i++)document.write(((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+'<br>');void(0);



bluefox
post May 22 2007, 11:33 PM

Getting Started
**
Group: Junior Member
Posts: 91

Joined: May 2007
i forgot i left the thread over here, i thought it was erased!

Thx, really appreciate it! rclxms.gif you all are wonderful!

those codes are rclxub.gif .. hehe but cool!

browser add bar.. thx for e tips, learn one new stuff again!


But i cant depict this one??

for(i = 1; i <= 100; i++) printf(t[3&19142723>>2*i%30], i);

"19142723" why that number?? was it some shifting binary or hexa?? i really can't depict that...

This post has been edited by bluefox: May 22 2007, 11:39 PM
sunsuron
post May 23 2007, 01:52 AM

olumis.com
******
Group: Senior Member
Posts: 1,320

Joined: Nov 2004
From: olumis.com



Glad u ask. Here are what everybody think about that kind of solution:
  • that wouldn't work
  • it's a literal - it means the number of flying monkeys.
  • magic-numbers are _always_ a mistake.
  • right.
  • whatever that means, it's fscking ridiculous.
  • let's do fizzbuzz contest!
  • maybe something like this may solve (i % 3) ? ( i % 5 ) ? FizzBuzz : Fizz : i ; (i % 5) ? (i % 3) ? FizzBuzz : Buzz : i
  • we always welcome interesting questions about C++, you don't need to ask if you can ask them. Additionally, if you wish to avoid being made fun of, read http://jcatki.no-ip.org/fncpp/HowToGetBetterHelp and http://www.catb.org/~esr/faqs/smart-questions.html.
  • oops sorry, that would work yes
  • that's a difficult problem
  • let's use geordi
  • Green potatoes?
Anyway, the author is Samuel Tardieu. See here.

This post has been edited by sunsuron: May 23 2007, 01:55 AM
nyem
post May 23 2007, 02:53 AM

Enthusiast
*****
Group: Senior Member
Posts: 705

Joined: Jan 2007


QUOTE(bluefox @ May 22 2007, 11:33 PM)
"19142723" why that number?? was it some shifting binary or hexa?? i really can't depict that...
*

I'm also at a loss. The magic number also work in javascript
CODE
var T = [0,'Fizz','Buzz','FizzBuzz'];
for (var i=1;i<=100;i++)     {
    T[0] = i;
    document.write( T[ 3&19142723>>2*i%30 ] + '<br>' );
}


and perl
CODE
perl -le "print+($_,Fizz,Buzz,FizzBuzz)[3&19142723>>2*$_%30]for 1..100"


The only explanation from Samuel Tardieu
QUOTE
Olive: I would only give the recruiter this kind of answer if I knew that she would appreciate it (being an ügergeek for example). The trick is to represent characteristics of every number (modulo 15) by an integer between 0 and 3 (plain, fizz, buzz, fizzbuzz). You need two bits for each number between 0 and 15, and can generate a decimal number with those 30 bits with: (thanks to J)
  4#.|.(-.*3|i.15)++:-.*5|i.15
19142723
(no, I don’t feel like explaining J today)

Then it’s simply a matter of choosing between the four alternatives after right shifting this large number by twice i (modulo 15) and using the two rightmost bits only.


Bump TopicReply to this topicTopic OptionsStart new topic
 

Switch to:
| Lo-Fi Version
0.0743sec    5.14    5 queries    GZIP Disabled
Time is now: 20th November 2017 - 11:22 PM