In this post, we’ll create a function to solve the overly popular FizzBuzz programming challenge.

By the end of this article, you will know:

  • How to create a function in Julia
  • How to do a for loop
  • How to create if-else blocks
  • What 1:5 means
  • How to calculate the remainder of a number when divided

This post is meant for beginner programmers or for those who never heard of Julia before. Don’t expect to do earth-shattering massively parallel scientific workloads after reading this. Consider this as your humble beginnings in the awesome world of Julia.


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

I guess you could play this game with any other 2 numbers, but 3 and 5 are the most popular choices.

This simple game has been translated to a programming challenge to torment beginner programmers. It also gives us 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.

I’ve previously solved FizzBuzz in BigQuery, so if you’re interested in knowing how you can solve this using a PB scale data warehouse, check out my article here.

Getting one step at a time

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 of what the function should achieve**. In our case, we want to print the first n numbers, but replacing numbers divisible by 3 with Fizz, 5 by Buzz and those that are divisible by both 3 and 5 (or 15 for you math geniuses out there) by FizzBuzz.

#programming-languages #programming #loop #julialang #beginners-guide

Learning Julia —the simplest of beginnings
1.10 GEEK