Andre van der Schyff

My online home and blog

FizzBuzz in a Tweet

Because I just know how to have a great time, I did a simple excercise tonight to see if I can implement FizzBuzz short enough to fit in a tweet. Some rules:

  1. It must be Java
  2. It must be a complete, runnable class
  3. It must print each entry on a new line
  4. It must…. well… fit in a tweet, so less than 140 characters

Here is the end result:


class f{public static void main(String[]s){for(int i=1;i<100;i++)System.out.println(((i%3==0)?"Fizz":"")+((i%5==0)?"Buzz":(i%3!=0)?i:""));}}

At exactly 140 characters, it works. It’s a pity that Java is so verbose that so much of it is taken up by the class and main method declaration. The gist of it is just 95 characters, and consists of a single expression made up of a for loop and some ternary string concatenation hackery. Even then, much of it consists of the println:


for(int i=1;i<100;i++)System.out.println(((i%3==0)?"Fizz":"")+((i%5==0)?"Buzz":(i%3!=0)?i:""));

This actually happened as a bit of a code kata. My first attempt was just an implementation that works without trying to be concise:


    void f1() {
        for (int i = 1; i < 100; i++) {
            boolean num = true;
            if (i % 3 == 0) {
                System.out.print("Fizz");
                num = false;
            }
            if (i % 5 == 0) {
                System.out.print("Buzz");
                num = false;
            }
            if (num) {
                System.out.print(i);
            }
            System.out.println("");
        }
    }

Attempt two shortened this by getting rid of the boolean and using String concatenation:

    void f2() {
        String s = "";
        for (int i = 1; i < 100; i++) {
            if (i % 3 == 0) s += "Fizz";
            if (i % 5 == 0) s += "Buzz";
            if (i % 5 != 0 && i % 3 != 0) s += i;
            s += "\n";
        }
        System.out.println(s);
    }

This actually fits in a tweet when compressed by removing whitespace. However it’s just a method, not a full class.

From here I realised that the if-statements can be compressed using ternary operators into a single statement that can be dropped directly into the println. By doing this, one of the checks can also be dropped from the last if, since it is already checked on the left had side of the ternary… and viola!

I did not cheat, but did do a quick google afterwards to see how short it can get. Here is one that fits into 44 characters using Perl… pretty impressive. Perhaps I’ll give it a shot using another language also, I think Groovy can once again be put to good use.

Challenge: Can this be shortened further while sticking to the rules?

,

3 Responses to “FizzBuzz in a Tweet”

  • Tim12s says:

    You can remove 6 of your parentheses due to operator precedence for a total of 134. My shortest in groovy is 33. (You can see below).

    [attempt 1]

    class f{public static void main(String[]s){for(int i=1;i<100;i++)System.out.println((i%3==0?"Fizz":"")+(i%5==0?"Buzz":i%3!=0?i:""));}}

    I tried a few "alternative" attempts to get it shorter and resorted to the following two approaches:

    [attempt 2]

    class f{public static void main(String[] s) throws Throwable {System.out.println(new String(sun.misc.IOUtils.readFully(new java.net.URL("http://ow.ly/fASpy&quot ;) .openStream(),-1,false)));}}

    And the following approach which breaks down due to the lack of an XMLHttpRequest object.

    [attempt 3]

    class f{public static void main(String[] s) throws Throwable {System.out.println(new javax.script.ScriptEngineManager().getEngineByName("javascript").eval("new XMLHttpRequest().open('GET','http://ow.ly/fASpy&#039;, false).send().responseText"));}}

    In groovy the solution is trivial:

    [attempt 4]

    'http://ow.ly/fASpy&#039;.toURL().text

    My solution is obviously quite a bit different than expected.

  • Tim12s says:

    It seems wordpress has replaced my single quote with a & # 0 3 9 ;

  • avdschyff says:

    Nice one @Tim12s … I’m not accepting the pastebin streaming ones of course but well done for sheer laziness-slash-creativity nonetheless :)

    Well spotted on the parentheses, I actually looked through it a few times and missed it completely.

    I wonder how short a legitimate groovy solution can get?

Leave a Reply