FizzBuzz Challenge

In this post, we'll attempt to create a function for the overly popular FizzBuzz programming challenge.

What is FizzBuzz?

If you've never heard of it, FizzBuzz is a simple counting game, aiming to teach kids multiplicative tables. Participants gather around in a cirle and someone starts counting from 1. The next person continues with 2 and so on, going around the circle. The trick is that if the number is divisible by 3, then instead of saying the number, the player should say Fizz. If the number is divisible by 5, then they should say Buzz. And if the number is divisible by both 3 and 5 then they should say FizzBuzz.

The above game has been translated a programming challenge for beginner programmers. This gives a good chance to practice for loops and if statements in Julia. Let's see how we can create a function that prints the solution for the first n numbers.

Instead of just running through the problem, let's go step by step and see how we can tackle this problem.

What are we doing?
I find it quite important to have a clear idea what the function should achieve. In our case, we want to print the first n numbers, but replacing numbers divisible by 3 with Fizz etc.

Examples?
Having an idea about what we want to achieve is all good but sometimes it's easier to understand a task via examples. What would our function output for n=3 or n=6? Writing these examples down will help us get to the solution and will also allow us to test our results.

So for n=3 we expect:
fizzbuzz(3)
1
2
Fizz
And for n=6 we want:
fizzbuzz(6)
1
2
Fizz
4
Buzz
Fizz

Also when we hit 15 we should say FizzBuzz, let's not forget about that either! :)

Let's start coding now. Since we're printing numbers one by one, some kind of looping mechanism will be needed. What if we just print the first n numbers and then change the ones we need?

function fizzbuzz(n)
for i in 1:n
println(i)
end
end

fizzbuzz (generic function with 1 method)


Obviously, this won't give us what we need, but at least we have some skeleton function to begin with. Let's test it:

julia> fizzbuzz(5)
1
2
3
4
5


Decent start!

Let's change the above so that we print Fizz for all numbers divisible by 3. How do we do this? Well, we can just check if something leaves 0 as reminder when dividied by 3. This is called a modulo of a number. There is a function for that - mod()!
Let's see how this works.

julia> mod(1,3)
1

julia> mod(2,3)
2

julia> mod(3,3)
0


Amending our function gives us:

function fizzbuzz(n)
for i in 1:n
if mod(i,3) == 0
println("Fizz")
else
println(i)
end
end
end

fizzbuzz (generic function with 1 method)

julia> fizzbuzz(5)
1
2
Fizz
4
5


Lookin' better! So now, whenever i leaves a remainder of 0 when divided by 3, we print Fizz instead i.

Now same logic applies to numbers divisible by 5. We just need to change the second argument of mod to 5.

Simples!

function fizzbuzz(n)
for i in 1:n
if mod(i,3) == 0
println("Fizz")
elseif mod(i,5) == 0
println("Buzz")
else
println(i)
end
end
end

fizzbuzz (generic function with 1 method)

julia> fizzbuzz(5)
1
2
Fizz
4
Buzz


Almost there! Last step is to add the case when the number is both divisble by 3 and 5. We could do this by adding another elseif clause and say mod(i,3)==0 & mod(i,5)==0 but we can also embed the clause inside the first (or second) if step. I'm going to do the latter, but this is just a matter or preference.

function fizzbuzz(n)
for i in 1:n
if mod(i,3) == 0
if mod(i,5) == 0
println("FizzBuzz")
else
println("Fizz")
end
elseif mod(i,5) == 0
println("Buzz")
else
println(i)
end
end
end

fizzbuzz (generic function with 1 method)


I prefer this way, as now we're only checking for mod(i,5) twice and we're not recalculating both mod(i,3) and mod(i,5).
But let's see if it worked:

julia> fizzbuzz(30)
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz