We have threads!

Join a laid-back, close-knit community of mixed interests Get a free account!

  1. FizzBang implementations

    #297802012-06-15 14:11:47Dave said:

    Hey guys, I recently came across the FizzBang (or FizzBuzz) problem that, as you know, is often used in programming job interviews.

    Here's my try in PHP, I came up with it in about 5 minutes. I'm aware it's not fully optimized, so I'm curious to see your implementations and optimizations!

    The problem is described like this:

    Code to output all numbers from 1 to 100. But if its a multiple of 3, output has to be Fizz instead. Multiple of 5, it has to be Bang. If its multiple of 3 and 5, FizzBang

    for($i = 1; $i < 101; $i++) {
        $of_three = ($i % 3 === 0);
        $of_five  = ($i % 5 === 0);
        if($of_three && $of_five)       echo "FizzBang\n";
        elseif($of_three && !$of_five)  echo "Fizz\n";
        elseif($of_five && !$of_three)  echo "Bang\n";
        else                            echo $i . "\n";


    You can make the site highlight your syntax prettily if you submit your code like this (in Markdown):


    Following syntax languages are supported:

    • php
    • javascript
    • shell
    • ruby
    • python
    • lua
    • c
    • scheme
    • c#
  2. #297832012-06-15 14:38:20 *Fieyr said:

    My javascript implementation.

    for( var i = 1; i  <= 100; i++){
    if(i % 3 === 0 && i % 5 === 0)
     else if(i % 3 === 0)
     else if(i % 5 === 0)
  3. #298102012-06-15 17:59:22 *CrimsonAlchemist said:

    I never heard of that game before ^^'

    Here I go : Two different implementation, one in C the other in Ruby The one in C is perf-oriented while the one in Ruby is much more flexible as you can add any rule (you simply add stuff to the Hashmap)

    #define FI 3
    #define BU 5
    #include <stdio.h>
    #include <stdlib.h>
      int i;
      for(i = 0;i<101;i++) {
        if(i%(FI * BU) == 0)
        else if(i%(BU) == 0)
        else if(i%(FI) == 0)
      return EXIT_SUCCESS;
    class Game
      attr_accessor :rule
      def initialize
        @ rule = {3=>"Fizz", 5=>"Bang"}
      def run()
        (1..100).each { |i|
          aff = ""
          @ rule.each { |key,v|
              aff << v if (i % key) == 0
          if aff.empty? then
              puts i
              puts aff
    g = Game.new

    (ruby support is a bit buggy, as it try to change the @ smthing into a link so I had to add a space between the @ and rule, sorry for the inconvenience)

  4. #298192012-06-15 18:51:25 *Fieyr said:

    I'm honestly not sure what the tradeoff would be. However, I do know that simple arithmetic operations are like... the most optimized things within programming languages. The CPU can process them incredibly quickly. If I had to guess, I would say that setting a variable is likely going to hurt performance...

    I would say that if your definition of optimized is speed... I would go crazy with the mods.

    If optimized means readable... then I think @Dave nailed it.

    Only way to know for sure would be to set up a timer and start and stop it before and after.

    And yeah, props to @CrimsonAlchemist. A FizzBuzz class. That's awesome :)

  5. #298242012-06-15 19:05:42 *Fieyr said:

    As far as removing control flow operators.... I was thinking the same thing. I wonder if there's some kind of predictable pattern that you could exploit to simply print according to a pattern. Almost like a fizzbuzz regex if that makes any sense. I think you would still have to do some kind of arithmetic, however you may be able to avoid having to do a mod... which is slower than a straight add or subtract.

    And speak of the devil, check out this c implementation that doesn't use mod.

    #include <stdio.h>
    void fizzbuzz(int num_fizz, int num_buzz, int num_fizzbuzz, int counter)
        if(num_fizzbuzz == 15) {printf("fizzbuzz "); num_fizz=0; num_buzz=0; num_fizzbuzz=0;}
        else if(num_fizz == 3) {printf("fizz "); num_fizz=0;}
        else if(num_buzz == 5) {printf("buzz "); num_buzz=0;}
        else printf("%d ", counter);
        if (counter < 100) fizzbuzz(++num_fizz, ++num_buzz, ++num_fizzbuzz, ++counter);
    main ()

    But yes it seems impossible to do without control operators.... fun to think about though.

  6. #298262012-06-15 19:14:44 *CrimsonAlchemist said:

    @Gargron sigh thanks god it's impossible ^^ If you had find something, I'd start thinking I was a complete nab :P

    @Fieyr lulzy, can be fun to compare the perfs of all the various program wrote here ^^

    well here is a funnier code I just write ^^

    res = []
    (1..100).each { |i|
        res[i-1] = i
    (1..33).each { |i|
        res[3*i-1] = "Fizz"
    (1..20).each { |i|
        res[5*i-1] = "Bang"
    (1..6).each { |i|
        res[15*i-1] = "FizzBang"
    (1..100).each { |i|
        puts res[i-1]


    Edit : lazy initialization lovers will die :P

    Edit 2 : oops puts res[i] is puts res[i-1] of course

  7. #298352012-06-15 21:47:38 *Xyopq said:

    Like a Boss

    for ($i = 1, $t = 1, $f = 1; $i <= 100; print "<br>", $i++, $t++, $f++) {
        if ($t != 3 && $f != 5) {
            echo "$i";
        } else {
            if ($t === 3) {
                echo "Fizz";
                $t = 0;
            if ($f === 5) {
                echo "Bang";
                $f = 0;
  8. #298392012-06-15 22:44:42 *Fieyr said:

    @Xyopq - Don't you need another if to spit out fizzbang? That took me a second to wrap my mind around though. I love it.

    Edit: OH WAIT! YOU DON'T! I love it even more now lol. Nice job.

  9. #298402012-06-15 22:56:45Fieyr said:

    @Xyoqp - Also! Instead of saying $t==3 for example... You could just say $t /3. This would return a 1 if $t is 3, which I assume would be treated as true in php.

  10. #298812012-06-16 08:57:12CrimsonAlchemist said:

    @Xyopq Nice job, this one (while close to) is much better than the C implem given by Fieyr (well I don't like the print inside the loop def however)
    For those who wonder why :
    1. not recursive (which is an improvement here)
    2. one less variable
    3. FizzBang is define thanks to Fizz and Bang (for example in the C example by Fieyr if you changed the conditions of Fizz you also had to change the condition on FizzBang which is bad)

    If it wasn't for PHP bad perf, it's probably the fastest algorithm until now

    @Deftones simply some geeks trying to solve a simple problem with many differents methods