📘
FizzBuzz
EasyMath & Design
Given integer n, return a list where for each number 1 to n:
- "FizzBuzz" if divisible by 3 and 5
- "Fizz" if divisible by 3
- "Buzz" if divisible by 5
- The number as string otherwise
Output each on a separate line.
Example:
Input:
15
Output:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
Reasoning:
- We start by iterating over the numbers from 1 to the input number, n=15.
- For each number i, we check its divisibility by 3 and 5, and apply the corresponding rules:
- if i is divisible by both 3 and 5, we output "FizzBuzz"
- if i is divisible by 3, we output "Fizz"
- if i is divisible by 5, we output "Buzz"
- otherwise, we output the number as a string.
- We repeat this process for all numbers from 1 to n, resulting in the output list with each item on a separate line.
- The rules are applied as follows:
- 3 is divisible by 3, so "Fizz" is output
- 5 is divisible by 5, so "Buzz" is output
- 15 is divisible by both 3 and 5, so "FizzBuzz" is output.
Constraints:
- 1 <= n <= 10^4
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.