Fizz Buzz — Day 55(Python)

Image for post
Image for post
Photo by Nick Fewings on Unsplash

Today’s question is somewhat easy. I thought since we were learning dynamic programming for quite some time, It was good to have a change and learn something easy. Let us look into the question.

412. Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three, it should output “Fizz” instead of the number, and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

To solve this problem we start a loop from 1, run it up to the required number. Each time we check if the current number is divisible by 15, 3, 5. Wait, why did I say we should check if the current number is divisible by 15? The question states the number should be divisible by 3 and 5 too. Since 15 is divisible by both 3 and 5, we check if the current number is divisible by 15.

Let us look into the code snippet.

Complexity analysis.

Time Complexity

We are running a loop from 1 to N, hence, the time complexity is O(N).

Space Complexity.

We are not using any extra data structure, hence the space complexity is O(1).

Software Engineer. Find me @ www.linkedin.com/in/annamariya-jt

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store