Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login
Fizz Buzz codegolf challenge in 15 languages (www.hackerrank.com) similar stories update story
27.0 points by rvivek | karma 1757 | avg karma 4.58 2012-12-14 16:23:07+00:00 | hide | past | favorite | 100 comments



view as:

Why does authorizing with github need permission to "Update my profile"?

The obvious scala implementation is 160 chars and works locally, but the site claims I score 0 for it:

     object Solution {def main(args: Array[String])=1 to 100 map {x=>System.out.print(if(x%3>0&&x%5>0)x else if(x%5>0)"Buzz" else if(x%3>0)"Fizz" else "FizzBuzz")}}

you are missing out new line characters after each output. `System.out.println` should work.

When I tried out the site, it gave me a score of 0 for submissions that compiled but produced the wrong output.

I've never written scala before but it looks like your logic is wrong.

Edit: I tweaked your code and got a score of 34

   object Solution {def main(args: Array[String])=1 to 100 map {x=>System.out.println(if(x%3==0&&x%5==0)"FizzBuzz" else if(x%5==0)"Buzz" else if(x%3==0)"Fizz" else x)}}

"You Scored: 56.00000000000001". :-)

How do you get javascript to work? I tried `process.stdout.write`, `console.log`, but the score is always zero.

Other issues:

- The "Solve FizzBuzz" button doesn't go anywhere if you are logged in, neither does "The Scenario" up top.

- Forum doesn't work (please fill in all the required fields - they already are)

- Signout link doesn't work


I'm wondering the same thing...

`console.log` works fine.

What is wrong with this then (not my entry):

    process.stdin.resume();
    process.stdin.setEncoding("ascii");
    process.stdin.on("data", function (input) {
        var output = ''
        if (input % 3 === 0){
            output += 'Fizz'
        }
        if (input % 5 === 0){
            output += 'Buzz'
        }
        console.log(output || input)
    });
Testing locally with

    for(i=1;i<=30;i++){
        process.stdin.emit('data', i.toString())
    }
    process.stdin.destroy()
gives the correct results.

There is no input. You have to print the desired result for all numbers between 1-100

Really? I just wasted 20 minutes trying to figure out whatever that boilerplate was supposed to receive :/

process.stdin.resume(); process.stdin.setEncoding('ascii');

process.stdin.on('data', function (input) { numbers = input.split("\n"); sum = parseInt(numbers[0]) + parseInt(numbers[1]) process.stdout.write(sum+"\n"); });

this is a sample code that takes a two digit integer and prints its sum. Can you use this format and see if it works?


for i in range(1,101): print "FizzBuzz" if i %3 == 0 and i % 5 == 0 else "Fizz" if i % 3 == 0 else "Buzz" if i % 5 == 0 else str(i)

Scores 69 in python but the login/signup is broken :)


117 is my best Python solution yet, but I'm struggling to get higher than that!

nvm it's just running realllllly slow...

Some simple ways to cut down on characters:

    for i in range(1,101):print 'FizzBuzz' if not i%15 else 'Fizz' if not i%3  else 'Buzz' if not i%5 else i

for i in range(1,101): print "FizzBuzz" if not i % 15 else "Fizz" if not i % 3 else "Buzz" if not i % 5 else str(i)

Even a little shorter :)


And a bit shorter still :D. Down to 102 chars now.

    for i in range(1,101):print "Fizz" if not i%3 else '' + "Buzz" if not i%5 else str(i) if (i%3) else ''
Edit: Down to 82 characters now. I'm not sure if this would work on every Python, but it works on mine.

    for i in range(1,101):print(("Fizz"if i%3==0 else'')+("Buzz"if i%5==0 else''))or i

Nice, The 82 character one works for me also, on Mac OS X python 2.7.3

You can drop the extra () which brings it down to 80.

  for i in range(1,101):print("Fizz"if i%3==0 else'')+("Buzz"if i%5==0 else'')or i

74

    for i in range(1,101):print(''if i%3 else'Fizz')+(''if i%5 else'Buzz')or i

70 now, still same idea.

    for x in range(1,101):print['','Fizz'][x%3==0]+['','Buzz'][x%5==0]or x

Excellent. Down to 68.

    for x in range(1,101):print['','Fizz'][x%3<1]+['','Buzz'][x%5<1]or x

My shortest Python solution is 59 characters. Two tricks, use while instead of range, and use string repetition instead of lists

  n=1
  while n<101:print'Fizz'*(n%3<1)+'Buzz'*(n%5<1)or n;n+=1
And here is a wacky one in 96 chars

  w=range(101)
  w[::3]=['Fizz']*34
  w[::5]=['Buzz']*21
  w[::15]=['FizzBuzz']*7
  for s in w[1:]:print s

You can save one more character with an exec loop

  n=1;exec"print'Fizz'*(n%3<1)+'Buzz'*(n%5<1)or n;n+=1;"*100

for i in range(1,101): print "FizzBuzz" if i % 15 == 0 else "Fizz" if i % 3 == 0 else "Buzz" if i % 5 == 0 else str(i)

derp, works better of course

Though I can't find out the score the page to test the code seems to be inaccessible if you are signed in. It just redirects you to the signed in page and the signout link is broken. Still it's a fun and cool idea


for i in range(1,101):print i%15==0 and 'FizzBuzz' or i%3==0 and 'Fizz' or i%5==0 and 'Buzz' or i has a score of 102

Redirects issue fixed. Can you try it now?

Works to login having trouble submitting however now

OK I cheated and used google and found (60 chars):

  for i in range(1,101):print'FizzBuzz'[i*i%3*4:8--i**4%5]or i
Pastebin of my explanation: http://pastebin.com/raw.php?i=00Db30FF

EDIT: ugh, sometimes I hate markdown, moved post to pastebin.


Awesomely ingenious use of math above. 59 if newlines count as 1.

    i=1
    while i<101:print'FizzBuzz'[i*i%3*4:8--i**4%5]or i;i+=1

Much simpler solution in 59 chars

  n=1
  while n<101:print'Fizz'*(n%3<1)+'Buzz'*(n%5<1)or n;n+=1

I came up with a shorter way to write the first index!

  n%-3&4
unbelievably it works

haha, clever!

Should have been empty string f[4:4]

It seems like an neat idea! But I'm not I'm sure if I would make whitespace count as characters. Seem like that punishes good formatting. I also scored 56.00000000000001 :D (I tried the Perl one)

118 for my PHP solution, will try and check if my Python skills can beat it.. btw I am not able to login and getting too many error .. HN effect ?

Yes, fixing it. Can you try?

same error ..will try after some time

Update : Logged in, Takes forever to submit


seems to be dead ... wonder if they are running the code submitted on the webserver & people have submitted busy loops or somesuch

There was a DDoS attack. Fixed.

how do we print in js?

process.stdout.write();

Hi,

can you check now. It is working. We run it in our codecheckers and not on webservers.


Cool, much faster now :)

I don't understand why this only gets a score of 1:

    static String[] c = { "", "FizzBuzz", "Buzz", "Fizz" };
    public static void main(String[] args)
    {
        for(int i = 1; i <= 100; i++)
        {
            c[0] = ""+i;
            int f = ((int)Math.ceil((i%3)/3.0))+1;
            int b = ((int)Math.ceil((i%5)/5.0))*2;
            System.out.println(c[(f+b)%4]);
        }
    }

Because it's huge and this is a code golf challenge?

=\ No point in trying with Java then?

You can do some decent golfing in Java. For improvement, how about removing all the casts, FP arithmetic, everything from Math, etc?

fizzbuzz and challenge in the same sentence? After the last guy I interviewed couldn't code Fibonacci or even explain how to do it, I think I lost my faith.

Bit confused by the ruby version. The signature you're given is:

    #!/bin/ruby

    # Head ends here
    def fizzbuzz_solve n

    end

    # Tail starts here 
I presumed this meant you were supposed to return a value of n, fizz, buzz or fizzbuzz for the value of n. When I tried to compile I got a score of 0 though. What are you supposed to do with the value of n, solve fizzbuzz for 1..n ?

In other languages n is the number between 1-100 passed into the method. The loop is outside of fizzbuzz_solve.

I presumed you just return the value and that the print was in the outer loop, seems you need to include the print statement in your method.

Sorry this is wrong. Removed the templates. You've to output from 1-100.

The boilerplate (include the shebang) counts towards your score, so just ditch it and start anew.

89 char in c

for(1..100){$_%15==0?print"FizzBuzz":$_%5==0?print"Buzz":$_%3==0?print"Fizz":print}

Is my best perl golf so far... wrote this one eons ago. The site doesn't seem to work well however and it won't let me submit it. Would obviously be shorter with "say" instead of "print."


This is the expected output (http://cdn.hackerrank.com/fizzbuzz.txt)

    say (1..100).map({ $_ %% (3|5) ?? ("Fizz" if $_ %% 3) ~ ("Buzz" if $_ %% 5) !! $_ }).join("\n")
is my quick stab at a Perl 6 version. About the same length if I take out the spaces (which I'd rather not!).

Perl 6 is so cool!

I like the bit

  $_ %% (3|5)
A pretty nifty way to condense multiple values into a single test.

I don't always perl, but when I do sometimes I like to golf because it's fun.


print(($_%3?"":Fizz).($_%5?"":Buzz)||$_,"\n")for 1..100

No need to quote the strings in some cases :-)


Just added a sample output. I think some of the code snippets are missing the "\n"

Login seems borked, but here's my Perl at 59 chars (141 points):

    print(($_%3?"":"Fizz").($_%5?"":"Buzz")||$_,"\n")for 1..100

What happens on login?

I tried the github option, and the first time I tried, it barfed about "unauthorized credentials" or some such after the redirect back from github, bringing me to another hackerrank login page. I tried it again, and it choked right away with the same message. Third time and it asks to "Confirm Submission", but hung after hitting yes.

Nice! Much better than my first stab. Slightly shorter (56 chars) P6 version:

   say(("Fizz" if $_%%3)~("Buzz" if $_%%5)||$_) for 1..100

This is highly amusing to me because my solution was almost exactly identical (and the same number of keystrokes:

  print(($_%3?"":"Fizz").($_%5?"":"Buzz")or$_,"\n")for 1..100
sadly, though it says they're running perl 5.14, it wouldn't work with 'say' which would have shaved off 7 characters.

edit: Guess what? The code above does not work. It's what I typed into my buffer, and then fixed it on the console to use || instead of or. Oh operator precedence!


My best C solution (scores a 96):

#define d printf(

main(i){i<101?(!(i%3)?d"Fizz"):0)|(!(i%5)?d"Buzz"):0)?d"\n"):d"%d\n",i),main(i+1):0;}


I was working on the same lines, and managed to bump it up to a score of 106:

  #define P printf(
  main(n){(n%3?0:P"Fizz"))+(n%5?0:P"Buzz"))?:P"%d",n);P"\n");n>99?:main(n+1);}

you can get rid of the !() if you reverse the ?:

  #define d printf(
  main(i){i<101?(i%3?0:d"Fizz"))|(i%5?0:d"Buzz"))?d"\n"):d"%d\n",i),main(i+1):0;}

excellent, missed that. :)

After 10 minutes, I was able to log in, but now I can't access the fizzbuzz page ):. Maybe I'll try later.


Fixed. Can you try it now?

It works, but I can't submit. the POST to https://www.hackerrank.com/rest/contests/fizzbuzz/challenges... eventually times out after 10 minutes.

I can however compile and verify my solution.


My best malbolge version:

    bCBA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{]s9wvutsU2ponmlNjihgfedcba`_^W{[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.n,%I)('&%$#"!~}|^zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-,+*j('&%$#z@~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHAeEDCBA:?>=<;:987654321*N.n,+*)('&%$#"!~}|{zyxwvutm32ponmlkjihgfedcb[!_X]VzZYXWVUTSRQ3ONMLKJIHGFEDCB;_?!=<;:981UT43210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&f$#"!x>|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-CgGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}|^zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHAeEDCBA:?>=<;:987654321*N.-,l*)('&%$#"!~}|u;:xwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJCBfeEDCBA@9>=<5Y9876543s10/.-,+*)('&%$#"!~}|u;y[wvo5mrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJCBfFEDCBA@?>7<;4X876/432+O/.-,+*)('g%|B"!~}|u;yxwvutsrqponmlkjihgfedcba`_^]V[ZYXWVUTSRQPImMLKJCHG@d'CB;_?!=<;:981UT43210/.-,+*)('&%$#"!~}|{z\xwvutsl2Sonmf,jihgfedcba`_^@\[ZYXQutTSRQPO1lFKJIHGFEDCBA@?>=<;:92V6543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjiKgfedcba`Y}]\[ZYXWVUTSRQPO1lLKDIHGFEDCBA@?>=<;4X876543s10)M-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=};:9876543210/.-,+*)('&%$#"!~}|u;sxwvutsrqj0nmfkjiha'&dcb[!_^]V[ZYXWVUTSRQPONMLKJIBfFEDCBA@?>=};:9870T43210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=};:3W765432r0/.-,%I)(!&}CB"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$d"!~}|{zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDC<A@?>=<;:98765.RQ10/.-,+*)('&%$#z!~}|{zyxwvutsrqj0hmf,jiha`&dcba`_^]\UTxXWVUTSRQPONMLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}v{zyr8vutslqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=};:3W76543s1*N.-,l*)('&%$#"!~}|u;:xwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-m+*)('~D$#"!~}|{z\xwp6tsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}|^zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDC<A@?>=<;:98765.R210/.n,+*)('&%$#"!~w={zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDC<A@?>=<;:98765.R210/.n,+*)('&%$#z@~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}v{zyr8vutslqj0/Plkd*hgfedFba`_^]\[ZYXWVUTSRKoONMLKJ,BfeEDCBA@?>=<;:3W76543210/.-m+*)('&%${Ay~}|{zyxwvo5srqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHA@dDCBA:?>=<;:92V654-210/.-,+*)('&%$#"!~}|{zyxwp6tsrqponmlkjihgfedcEa`_^]\[ZYXQuUTMRQPONMFjJ,BfeEDCBA@?>=<;:3W76543210/.-m+*)('&%${A!~wv<zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=};:3W76543s1*N.-,l*)('&%$#"!~}|u;:xwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9y76543210/.-,%I)('&%$#"!~}|^zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHA@dDCBA@?>=<;:9876543s10)M-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHA@dDCBA@?>=<;:9876543210/.n,+*)(!E%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$d"!~}|{zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHAeEDCBA:?>=<;:987654321*N.-,l*)('&%$#"!~}|u;:xwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-m+*)('&%$#"!~}|{zyr8vutsl2jonmlkjihgfedcb[!_X]VzZYXWV8TSRQPONMLKJIHGFED=aA@?>=6;:3WV6543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-m+*)('&%$#"!~}|{zyr8vutsrqpih.lkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-m+*)"FE%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}|^zyr8vutmrqj0/Plkd*hgfedFba`_^]\[ZYXWVUTSRKoONMLKJ,BfeEDCBA@?>=<;:3W76543210/.-m+*)('&%$#"!~}|{zyr8vutsrqponmlkjihafedcb[!_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-m+*)"F&f$#"!x>|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&}$#"!~}|{t:xqvutm32ponmlkjihgfedcb[!_^]\[TYXWVUTSRQPONMLKJIBfFEDCBA@?>=};:9870T43210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=};:3W765432r0/.-,%I)(!&}CB"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$d"!~}|{zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&f$#"!~}|{zyxwvutsrqpong-Njihgf_%cba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"b~w={zyxwvutsrqponPlkjihgf_%cba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('g%$#"!~}|{zyxq7utslqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHAeEDCBA:?>=<;:987654321*N.-,l*)('&%$#"!~}|u;:xwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!~`|{zyxq7utsrqponmfkjihgfedcba`Y}]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)(h&%$#"!~}|{zs9wvutsrqponmfkjiha'&dcb[!_^]\UZYXWVUTSRQPONMLKJIBfFEDCBA@?>=<;:92V6543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFED&BA@?>=6Z:9876543210/.n,+*#G'&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}|^zyr8vutmrqj0/Plkd*hgfedFba`_^]\[ZYXWVUTSRKoONMLKJ,BfeEDCBA@?>=<;:3W76543s10/.-,+*)"F&%$#"!~}|uzyr8vutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDC%A@?>=6Z:9876543210/.-,+$H('&%$#"!~}|{zyxwvutsrqjonmlkjihgfedcb[!_X]VzZYXWV8TSRQPONMLKJIHGFED=aA@?>=6;:3WV6543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9y70T.3210/.-,+*)('&%${A!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHAeEDCBA@?!=<;:9876543210/.-,%I)(!&}CB"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=};4X87654321*/.-&J*)('&%$#"!~`v<;yxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9y1U543s10)M-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcEa`Y}]\[ZSXWVUTSRQPONMLKDhHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.n,%I)('&%$#"!~}|^zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHAeEDCBA:?>=<;:987654321*N.-,l*)('&%$#"!~}|u;:xwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDC%A@?>=<;:981U543210/.-,+$H('&%$#"!~}|{zyxwvutsrqjonmlkjihgfedcb[!_X]VzZYXWV8TSRQPONMLKJIHGFED=aA@?>=6;:3WV6543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9y1U5.3210/.'K+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&f$#"!x>|u;yxwvunsrqpi/glkjihgfedcba`_^]VzZYXWV8TSRQPONMLKJIHGFED=aA@?>=6;:3WV6543210/.n,+$H(h&%$#"!~}|{zs9Zvutm32ponmlkjihgfedcb[!_^W\[TxRQuUTSRQPO1lLKJIHGFEDCBA@?>=<;:9y1UT43210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVU7rRQPONMLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}|^zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543s10)M'&J*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHAeEDCBA:?>=<;:987654321*N.n,+*)('&%$#"!~}|{zyxwvutm32ponmlkjihgfedcb[!_X]VzZYXWVUTSRQ3ONMLKJIHGFEDCB;_?!=<;:981UT43210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:987654t210/.-,+*)(!E}$#"!~}|{zyr8vutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-CgAFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{zyxwvo5srqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}|^zyr8vutsrkj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHAeEDCBA:?>=<;:987654321*N.-,l*)('&%$#"!~}|u;:xwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-CgGFEDC%A@?>=6Z:9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}|^zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-CgfFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-CgG@EDCBA@?>=<;:981U543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}|^zyr8vutmrqj0/Plkd*hgfedFba`_^]\[ZYXWVUTSRKoONMLKJ,BfeEDCBA@?>=<;:3W76543210/.n,+*#G!&%$#z@~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDC%A:^>=<;:9876543210/.-m+*)('~D$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFEDCBA@?>=<;:9876543210/.-,%I)('&%$#"!~}|^zyr8vutmrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHG@dDCBA@?>=<;:3876543210/.-,+*)('&%${A!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHAeEDCBA:?>=<;:987654321*N.-,l*)('&%$#"!~}|u;:xwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLKJIHGFEDCBA@?>=<;:9876543s10/.-,+*)('&%$#"!~}|u;y[wvo5mrqj0/mlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHGFED=aA:?>=<;:981U543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lLK-IHG@dDCBA@?>7[;49876543210/.-,+$H('&%$#"!~`|u;sxwvutsrqj0nmfkjiha'&dcb[!_^]\[ZYXWVUTSRQPONMLK-IHAeEDCBA@?!=<;:92V6543,+ON.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPO1lY

I'd never heard of malbolge until last night, when it played a role in the mystery on CBS' "Elementary" (an excellent series, BTW). Kind of weird to encounter such an obscure language twice in under 24 hours.

I don't know why I'm such a sucker for this sort of thing. I couldn't get it to score me, but here's 104 chars of Clojure:

    (for [i(range 100):let[d #(= (mod i %) 0)]](println(cond(d 15)"FizzBuzz"(d 3)"Fizz"(d 5)"Buzz":else i)))

71 characters (129 points) with JavaScript. I actually wrote this quite a while back (and have also posted it on HN before) - code golf is pretty fun every now and then!

    for(var i=0;i++<100;console.log(((i%3?'':'Fizz')+(i%5?'':'Buzz'))||i));
For anyone else interested in doing code golf with JavaScript, here's a goldmine of byte-saving techniques:

https://github.com/jed/140bytes/wiki/Byte-saving-techniques


Good, now everyone can just copy & paste it and the competition is a tie ;)

I came up with

    for(i=0;i<100;)console.log((++i%3?"":"Fizz")+(i%5?"":"Buzz")||i)
earlier. 64 characters, because I don't have "var ", an extra set of parens, and a semicolon. Worse syntax, but hey, it's code golf.

I knew I could also drop the "var ", but I like to operate within the 140bytes challenge rules that say you're not allowed to leak into the global scope. I'll give you the 3 chars from the parens and semicolon, though - I got caught up with the idea of "hey, you can do this whole thing inside the for statement itself!" back when I did this!

Mine was almost identical, except with i++<100 instead of ++i%3? etc. Also 64 characters.

But is there really no way to end the ternary operators without parenthesis?


Erlang version (more verbose):

    lists:map(fun(N)->io:fwrite("~s~n",[if N rem 15==0->"FizzBuzz";N rem 3==0->"Fizz";N rem 5==0->"Buzz";true->integer_to_list(N)end])end,lists:seq(1,100)).

Couldn't get it to run but here is my best in ruby:

1.upto(100).each {|x|s="#{'Fizz' if x%3 == 0}#{'Buzz' if x%5 == 0}";s=="" ? p(x): p(s)}


# Perl at 51 characters (149 points).

print((Fizz)[$_%3].(Buzz)[$_%5]||$_,"\n")for 1..100


Go (golang) - 157 characters/43 score

  package main
  import(o"fmt")
  func main(){s:=o.Print
  for i:=1;i<101;i++{f,b:=i%3==0,i%5==0
  if!(f||b){s(i)}else{if f{s("Fizz")}
  if b{s("Buzz")}}
  s("\n")}}
Compiles and runs fine but obviously not go fmt compliant. I feel bad for even attempting it.

Seems like the score should vary by language, Go (thankfully) makes it hard to be too terse. And of course in Java you're doomed prior to writing a single line of real logic code


Couldn't resist:

  class Solution{public static void main(String[] a){for(int i=0;++i<101;)System.out.println(i%15==0?"FizzBuzz":i%3==0?"Fizz":i%5==0?"Buzz":i);}}
143 characters, 57 score

Nice. Lack of the ternary operator is the big sticking point I have with really compact Go solutions.

OTOH, in real world code I'm glad to see it gone.

Also just noticed the () on my import aren't needed, first one would have to be a space anyway but second one can be removed to get the character count down by one and a bit more jiggering of the if/else logic can shave another character:

  package main
  import o"fmt"
  func main(){s:=o.Print
  for i:=1;i<101;i++{if f,b:=i%3==0,i%5==0;(f||b){if f{s("Fizz")}
  if b{s("Buzz")}}else{s(i)}
  s("\n")}}

Looks like I can't submit my code. I can run it, but not submit it.

my best c++ solution scores 75 points with 126 characters

    #include<iostream>
    main(){for(int i=0;i++<100;){if(i%5&&i%3)std::cout<<i;std::cout<<(i%3?"":"Fizz")<<(i%5?"":"Buzz")<<'\n';}}


Haskell - 112 bytes / 88 score

  a="Fizz"
  b="Buzz"
  i%j=mod i j==0
  f i
   |i%15=a++b
   |i%5=b
   |i%3=a
   |True=show i
  main=mapM_ (putStrLn.f) [1..100]

Legal | privacy