FizzBuzz

From Software Engineers Wiki
Jump to: navigation, search

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".

Contents

Answer #1

First, the simple code.

#include <stdio.h>

int main(void)
{
        int i;

        for (i = 1; i <= 100; ++i) {
                if (i % 15 == 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);
        }

        return 0;
}

Above code uses modulus operator, which turns to a multiply operation, which is costly. For example, (i % 3 == 0) code is compiled to

  • x86 32 bits
  31:   89 d8                   mov    %ebx,%eax
  33:   ba 67 66 66 66          mov    $0x66666667,%edx
  38:   f7 ea                   imul   %edx
  3a:   d1 fa                   sar    %edx
  3c:   29 ca                   sub    %ecx,%edx
  3e:   8d 04 92                lea    (%edx,%edx,4),%eax
  41:   39 c3                   cmp    %eax,%ebx
  43:   75 53                   jne    98 <main+0x98>
  • ARM 32 bits
   c:   e59f70a0        ldr     r7, [pc, #160]  ; b4 <main+0xb4>
...
  2c:   e0c21497        smull   r1, r2, r7, r4
  30:   e06330c2        rsb     r3, r3, r2, asr #1
  34:   e0833103        add     r3, r3, r3, lsl #2
  38:   e1540003        cmp     r4, r3
  3c:   e1a01004        mov     r1, r4
...
  44:   1a000013        bne     98 <main+0x98>

In Python,

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

main()

Answer #2

For the sake of performance, it is better to eliminate the modulus operation.

#include <stdio.h>

int main(void)
{
        int i;
        int count3, count5;

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

        return 0;
}

This still works, but it has too many conditional statements. We can simplify the logic.

Answer #3

#include <stdio.h>

int main(void)
{
        int i;
        int count3, count5;
        bool print_num;

        for (i = 1, count3 = 3, count5 = 5; i <= 100; ++i) {
                --count3;
                --count5;
                print_num = true;
                if (count3 == 0) {
                        printf("Fizz");
                        count3 = 3;
                        print_num = false;
                }
                if (count5 == 0) {
                        printf("Buzz");
                        count5 = 5;
                        print_num = false;
                }
                if (print_num)
                        printf("%d", i);
                printf("\n");
        }

        return 0;
}

In Python,

import sys

def main():
        count_3 = 3
        count_5 = 5
        for i in range(1, 101):
                count_3 -= 1
                count_5 -= 1
                print_num = True

                if (count_3 == 0):
                        sys.stdout.write("Fizz")
                        count_3 = 3
                        print_num = False
                if (count_5 == 0):
                        sys.stdout.write("Buzz")
                        count_5 = 5
                        print_num = False
                if (print_num):
                        print(i)
                else:
                        print("")

main()

Answer #4

Or we can use a pre-defined table which is an array of strings.

#include <stdio.h>

static char str_fizz[] = "Fizz";
static char str_buzz[] = "Buzz";
static char str_fizzbuzz[] = "FizzBuzz";
static char *str_15[] = {
        NULL,
        NULL,
        str_fizz,
        NULL,
        str_buzz,
        str_fizz,
        NULL,
        NULL,
        str_fizz,
        str_buzz,
        NULL,
        str_fizz,
        NULL,
        NULL,
        str_fizzbuzz,
};

int main(void)
{
        int i;
        char **str = &str_15[0];

        for (i = 1; i <= 100; ++i) {
                if (*str == NULL)
                        printf("%d\n", i);
                else
                        puts(*str);
                if (*str == str_fizzbuzz)
                        str = &str_15[0];
                else
                        ++str;
        }

        return 0;
}

In Python,

def main():
        str_15 = ("", "", "Fizz", "", "Buzz", "Fizz", "", "", "Fizz", "Buzz", "", "Fizz", "", "", "FizzBuzz")
        str_index = 0

        for i in range(1, 101):
                if str_15[str_index] == "":
                        print(i)
                else:
                        print(str_15[str_index])
                str_index += 1
                if str_index == 15:
                        str_index = 0

main()
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox