Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

90% of /prog/ can't write FizzBuzz

Name: The antagonist 2008-04-25 12:38

Prove me wrong

Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

Name: Anonymous 2008-04-30 21:57

>>155
I cry because I'm actually forced to write shit like this.

Name: Anonymous 2008-04-30 23:07

>>160
You can write ENTERPRISE in any language.

Name: Anonymous 2008-04-30 23:30

>>161
ONE WORD, THE FORCED WRITING OF SHIT, THREAD OVER

Name: Anonymous 2008-05-01 2:40

>>162
Even Scheme?

Name: Anonymous 2008-05-01 2:40


int main() {
  static int i = 1;
  if (!(i%3)) printf("Fizz");
  if (!(i%5)) printf("Buzz");
  if (i%3 || i%5) printf("%d",i);
  putchar('\n');
  i++;
  if (i<=100) main();
  return(0);
}

Name: Anonymous 2008-05-01 3:05

>>165
* changed || to && (was causing erroneous behavior)
* removed redundant value checks
* removed the static variable (poor form)
* added declarations for printf and putchar
* tail-call optimized

#include <stdio.h>
int main(int i, char **argv) {
    return argv ? main(1,NULL) : i > 100 ? 0 : ((
        (i%3 || (printf("Fizz"), 0))
        && (i%5 || (printf("Buzz"), 0))
    ) && printf("%d",i), putchar('\n'), main(i + 1, NULL));
}

Name: Anonymous 2008-05-01 3:10

>>166
: ((
it looks like shit now. I hope you feel kind of bad about it。。。

Name: Anonymous 2008-05-01 3:20

>>167
You mean it looks like Lisp now, or about as close as you'll get in C.

Here's a version with a bunch of extra return statements, maybe it's more to your liking.


#include <stdio.h>
int main(int i, char **argv) {
    if (argv)
        return main(1, NULL);
    if (i > 100)
        return 0;
    (
        (i%3 || (printf("Fizz"), 0))
        && (i%5 || (printf("Buzz"), 0))
    ) && printf("%d",i);
    putchar('\n');
    return main(i + 1, NULL);
}

Name: Anonymous 2008-05-01 4:20

>>166
EXPERT DOCUMENTATION WRITER

Name: Anonymous 2008-05-01 11:54

>>166
That was so beautiful I had to post a version that actually works (I apologize if it was meant to be wrong).

#include <stdio.h>
int main(int i) {
    return i > 100 ? 0 : ((
         (!(i%3) && printf("Fizz"))
       + (!(i%5) && printf("Buzz"))
    ) || printf("%d",i), putchar('n'), main(i + 1));
}

Name: Anonymous 2008-05-01 12:36

this thread is way too fucking long, can anyone just highlight the more interesting implementations tia

Name: Anonymous 2008-05-01 13:22

>>171
There is nothing interesting in FizzBuzz

Name: Anonymous 2008-05-01 13:30

>>172
False. They did this on the Mythbusters.

Name: Anonymous 2008-05-01 13:56

>>173
FizzBuzters?

Name: Anonymous 2008-05-01 14:07

>>171
I think these are the most interesting: >>28,37,76,153,170

Name: Anonymous 2008-05-01 14:11

>>170
Hm? I tested mine before I posted it. Even built with -ansi -pedantic -W -Wall and tried it on Linux/AMD64, FreeBSD/i686, and PPC/OS X. GCC 4.3 prints two warnings, but it produced correct output on all three systems.

Name: Anonymous 2008-05-01 14:25

>>176
Does not print "FizzBuzz" on multiples of 15 because "&&" is shorting. Should have used "*" instead.

Name: Anonymous 2008-05-01 14:41

>>177
Or [code]&[\code], which is more efficient.

Name: Anonymous 2008-05-01 14:54

>>177
Oh fuck, I pasted the wrong version. That should've been &, as >>178 said. [/dumbass]

Your version doesn't work if you give it command line arguments. (and you have 'n' instead of '\n', but that's more a typo than an implementation error)

Name: Anonymous 2008-05-01 15:08

>>179
I test-posted it on http://snafu.dasourcerer.net/bbcode/ and it ate the backslash. "&" might be dangerous because (1&2) is 0, thus (7%3)&(7%5) is 0 too. It does work on your program, though. I sacrificed correct command line argument handling to achieve increased beauty. It's fucking beautiful, dude. Congratulations.

Name: Haprog 2008-05-01 17:00

I challenge you to write FizzBuzz in Brainfuck

Name: Anonymous 2008-05-01 17:06

print "\n".join(([str((o,o,f,o,b,f,o,o,f,b,o,f,o,o,z)[o%15]) for (f,b,z) in (("Fizz", "Buzz", "FizzBuzz"),)])[0] for o in range(1,100))

Name: Anonymous 2008-05-01 17:09

>>181
I'll try to write one in befunge.

Name: Anonymous 2008-05-02 5:44

#include <stdio.h>
#include <conio.h>

int main()
{
    int i;
    for(i = 1; i <= 100; i++)
    {
          if((i%3) == 0 && (i%5) == 0)
          {
           printf("FizzBuzz\n");
          }
          else if((i%3)==0)
          {
           printf("Fizz\n");
          }
          else if((i%5)==0)
          {
           printf("Buzz\n");
          }
          else
          {
           printf("%d\n",i);
          }
    }
    getch();
    return 0;  
}

Name: Anonymous 2008-05-02 7:12

#include <stdio.h>

int main(){
 for(int i = 1; i < 101; ++i){
  int j = i % 15;
  ((!j || (j & 3 ^ j >> 2) == 3) && printf("Fizz")) |
  ((j & 3) == j >> 2 && printf("Buzz")) ||
  printf("%d",i);
  puts("");
 }
 return 0;
}

Name: Anonymous 2008-05-02 7:29

>>185
EXPERT bit twiddler detected.

Name: Anonymous 2008-05-04 20:31

Will run on GForth.

: fizzbuzz
  101 1 +do
    r@ 3 mod 0= dup if ." Fizz" endif
    r@ 5 mod 0= dup if ." Buzz" endif or
    invert if r@ . endif
    cr
  loop ;

fizzbuzz bye

Name: Anonymous 2008-05-05 0:51

I created this language called FizzBuzz.  It has one keyword, FizzBuzz.  It also supports C-style comments and blocks (braces), and statements must be terminated with a semicolon.  The FizzBuzz keyword creates the output as described in the OPs post.  Here is the program:


# DO_OP_BIDDING.FBZ: Do what OP said - Version 1.0
{ FizzBuzz };

Name: Anonymous 2008-05-05 0:52

>>188
Whoops.  Changed my mind.  Does not support C-style comments, but rather Unix style comments.

Name: Anonymous 2008-05-05 1:29

>>189
That so ruined everything. You made a dynamic entry and then on the way out you slipped and fell on your ass and everybody lold and then commenced to hax your anus.

Name: Anonymous 2008-05-05 15:29

>>153
holy shit that's nice.

Name: Anonymous 2008-05-05 15:42

>>191
#define nice "ugly as motherfuck"

Name: Anonymous 2008-05-05 17:58

>>153
Excuse me fine sir, we can't let you board the LOLplane with that extra piece of whitespace. You'll have to stow it in the whitespacehold with the rest of the whitespace:

print"\n".join(map(lambda(x):str([x,"Fizz","Buzz","FizzBuzz"][(x%3==0)+(x%5==0)*2]),range(1,101)))

Name: Anonymous 2008-05-05 19:52

#include <iostream>
int main()
{
  char buf[32]; char *strs[4]={"","Fizz","Buzz","FizzBuzz"};
  for(int c=1;c<=100;c++) std::cout<<((((c%3==0?1:0)+((c%5==0)?2:0))>0)?strs[(c%3==0?1:0)+((c%5==0)?2:0)]:itoa(c,buf,10))<<' ';
  return 1;
}

Name: Anonymous 2008-05-05 19:57

>>194

goddamnit I didn't see 153.

Name: Anonymous 2008-05-05 20:03

Must admit, I am impressed by the Perl and Python one liners. Can't us Haskellers make a short one liner?

Name: Anonymous 2008-05-05 20:16

After closer inspection it seems that they exploit that boolean operators return numbers instead of true or false typed values. Haskell can't do this of course. Otherwise something similar like this might work:

mapM_ putStrLn$map(\x->[x,"Fizz","Buzz","FizzBuzz"]!!(x`mod`3==0)+(x`mod`5==0)*2)[1..101]

Name: Anonymous 2008-05-05 20:34

>>194
Here I fixed your sepples for you:

#include <stdio.h>
int main()
{
  char*s[4]={"%d ","Fizz ","Buzz ","FizzBuzz "};
  for(int c=1;c<=100;c++)printf(s[(c%3==0)+((c%5==0)<<1)],c);
  return 0;
}

Name: Anonymous 2008-05-05 20:42

>>196,197
Thy wish be granted.

mapM_ putStrLn$map(\x->["FizzBuzz","Buzz","Fizz",show x]!!(signum(x`mod`3)+signum(x`mod`5)*2))[1..100]

I still like >>37 better, though.

Name: Anonymous 2008-05-05 20:58

These versions will save you 3 more characters.

mapM_(\x->putStrLn(["FizzBuzz","Buzz","Fizz",show x]!!(signum(x`mod`3)+signum(x`mod`5)*2)))[1..100]

mapM_(putStrLn. \x->["FizzBuzz","Buzz","Fizz",show x]!!(signum(x`mod`3)+signum(x`mod`5)*2))[1..100]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List