FizzBuzz
Turn the numbers 1 to n into Fizz, Buzz, FizzBuzz or the number itself, using remainders after dividing by 3 and 5.
Given a whole number nini
if"FizzBuzz" is divisible by both 3 and 5;i if"Fizz" is divisible by 3;i if"Buzz" is divisible by 5;i- the number itself, written as a string, otherwise.
Example 1
Output: ["1","2","Fizz","4","Buzz"]
Example 2
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
The Idea
Picture FizzBuzz as a counting game for a group of players. They take turns counting up from 1, one number each. Instead of a number that 3 goes into evenly, a player says "Fizz", and instead of one that 5 goes into evenly, "Buzz". Here the program plays every turn and writes the answers into a list.
Follow Example 2 from the start. The numbers 1 and 2 stay as they are, 3 becomes Fizz, 4 stays, and 5 becomes Buzz. After that, 6, 9 and 12 are Fizz and 10 is Buzz. Then comes 15. Three goes into 15 five times and 5 goes into it three times, so 15 fits both rules. Which word should it get? The statement settles it: both words together, FizzBuzz.
How can a program tell whether 3 goes into a number evenly? It divides and looks at what is left over, called the remainder. Java's %7 % 39 % 3
Each column is one number. The row labelled "remainder after ÷ 3" has a blue 0 under 3, 6, 9, 12 and 15, and those are the Fizz columns of the output row. The row labelled "remainder after ÷ 5" has a teal 0 under 5, 10 and 15, the Buzz columns. Column 15, framed in orange, has a 0 in both rows, and its output shows Fizz and Buzz together.
So every number falls into one of four cases: both remainders are 0, only the first is, only the second is, or neither is. The case to handle with care is the first one, because a number like 15 also passes each single test on its own.
Solution Approach
The code needs three decisions: the order of the tests, how to test for 3 and 5 at once, and how to put a plain number into a list of strings. Building each word from two pieces is an optional way that needs neither the careful order nor the test for 15.
Order of the tests
Which test should come first? The tests sit in an ifelse if
So the "both" case goes first, followed by the tests for 3 and for 5. Once "both" is ruled out, a number can pass at most one of those two, so their order does not matter.
Testing for 3 and 5 at once
How do we test for both? Writing i % 3 == 0 && i % 5 == 0i % 15 == 0
That shortcut needs the two numbers to share no factor. With 4 and 6, which share the factor 2, the number 12 is divisible by both, yet 4 × 6 = 24 does not go into it.
Numbers as text
What goes into the list for a number like 7? The list holds strings, so the number is turned into text with String.valueOf(i)"7"
Optional: building each word from two pieces
Is there a way without a separate test for 15? Start each entry as an empty string, add "Fizz" when the remainder after dividing by 3 is 0, then add "Buzz" when the remainder after dividing by 5 is 0. For 15 both pieces get added, in that order, which spells FizzBuzz. If nothing was added, the entry is the number itself.
This version suits a game with more rules, such as a third word for multiples of 7, because each rule is one more line and no combined tests are needed. A run that compared both versions gave the same list for every n
Java Solution
The method fizzBuzz
- First, create an empty
namedArrayList to hold the entries.answer - A
loop movesfor from 1 up toi , includingn itself.n - For each
, ani -if chain testselse if first and adds "FizzBuzz", theni % 15 == 0 for "Fizz", theni % 3 == 0 for "Buzz".i % 5 == 0 - When none of those three tests passes, the last branch adds
, the number as text.String.valueOf(i) - After the loop, return
.answer
The mainn = 1n = 0n = 45
import java.util.ArrayList;
import java.util.List;
public class FizzBuzz {
public static List<String> fizzBuzz(int n) {
List<String> answer = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i % 15 == 0) {
answer.add("FizzBuzz"); // a multiple of 3 and of 5
} else if (i % 3 == 0) {
answer.add("Fizz");
} else if (i % 5 == 0) {
answer.add("Buzz");
} else {
answer.add(String.valueOf(i)); // the number itself, as text
}
}
return answer;
}
public static void main(String[] args) {
System.out.println(fizzBuzz(5));
System.out.println(fizzBuzz(15));
System.out.println(fizzBuzz(1));
System.out.println(fizzBuzz(0));
List<String> upTo45 = fizzBuzz(45);
System.out.println("30: " + upTo45.get(29) + ", 45: " + upTo45.get(44));
}
}
Running it prints:
[1, 2, Fizz, 4, Buzz]
[1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz]
[1]
[]
30: FizzBuzz, 45: FizzBuzz
Java prints a list of strings without quotes, so [1, 2, Fizz, 4, Buzz]n = 1[1]
With n = 0i <= n[]
What if the test for 3 came first in the chain? A run with the tests in the order 3, 5, 15 gives [11, Fizz, 13, 14, Fizz]
Complexity
Each number from 1 to n
- Time: O(n), because the loop runs once for each of the
numbers, and each pass does at most three remainder tests and onen to the list.add - Space: O(n) for the answer, because the list holds one string per number, while the method itself keeps only the counter
.i
Another short exercise built on remainders is