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:
Anonymous2008-04-30 21:57
>>155
I cry because I'm actually forced to write shit like this.
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:
Anonymous2008-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:
Anonymous2008-05-01 3:10
>>166 : ((
it looks like shit now. I hope you feel kind of bad about it。。。
Name:
Anonymous2008-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);
}
>>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));
}
>>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:
Anonymous2008-05-01 14:25
>>176
Does not print "FizzBuzz" on multiples of 15 because "&&" is shorting. Should have used "*" instead.
>>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:
Anonymous2008-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:
Haprog2008-05-01 17:00
I challenge you to write FizzBuzz in Brainfuck
Name:
Anonymous2008-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))
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:
Anonymous2008-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:
Anonymous2008-05-05 0:52
>>188
Whoops. Changed my mind. Does not support C-style comments, but rather Unix style comments.
Name:
Anonymous2008-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.
>>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:
Must admit, I am impressed by the Perl and Python one liners. Can't us Haskellers make a short one liner?
Name:
Anonymous2008-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:
>>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:
Anonymous2008-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]