v9.1.1
Menu

FizzBuzz Classic

easyFREEControl FlowPython Control FlowPython Foundations

Problem Statement

Implement the classic FizzBuzz algorithm.

Background

FizzBuzz is a common programming challenge:

  • Print "Fizz" for multiples of 3
  • Print "Buzz" for multiples of 5
  • Print "FizzBuzz" for multiples of both
  • Otherwise print the number

Your Task

Write a function fizzbuzz(n) that returns a list of FizzBuzz results from 1 to n.

Output Format

Return a list of strings.

Example

Input
n = 15
Output
['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz']
Reasoning
  • Input: n =15= 15 is the inclusive upper limit, so the output covers every integer kk from 11 to 1515 — exactly 15 entries, one per kk, in increasing order.
  • Multiples of 3 only: 3,6,9,123, 6, 9, 12 leave remainder 00 when divided by 33 but not by 55, so those positions hold Fizz.
  • Multiples of 5 only: 55 and 1010 are divisible by 55 but not by 33, so they become Buzz.
  • Multiples of both: 15=3×515 = 3 \times 5 is divisible by 33 and 55 (equivalently by lcm(3,5)=15\mathrm{lcm}(3,5) = 15). It also satisfies the "multiple of 3" and "multiple of 5" rules, but the combined rule wins, so the last entry is FizzBuzz, not Fizz.
  • Everything else: 1,2,4,7,8,11,13,141, 2, 4, 7, 8, 11, 13, 14 match no rule, so they appear as themselves — but as text, e.g. '1', not the number 11, since the result is a list of strings.
  • Notice that FizzBuzz appears once per 15 numbers; for n =5= 5 the list ends at Buzz and never reaches it.

Constraints

  • n >= 1
  • Check divisibility by both 3 and 5 first

Learn this in

More from Python Foundations

solution.py

Test results

Sign in to run your code against the test cases.