From 12e2addd813ec3b9d92bdaf4227b8f1875700b64 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 20 Mar 2021 11:02:29 -0700 Subject: [PATCH] Add naive approach --- maths/fizz_buzz.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 maths/fizz_buzz.rb diff --git a/maths/fizz_buzz.rb b/maths/fizz_buzz.rb new file mode 100644 index 0000000..b35d5ee --- /dev/null +++ b/maths/fizz_buzz.rb @@ -0,0 +1,56 @@ +# 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”. + +# +# Approach 1: Naive Approach +# + +# Complexity Analysis + +# Time Complexity: O(N) +# Space Complexity: O(1) + +# @param {Integer} n +# @return {String[]} +def fizz_buzz(n) + str = [] + + n.times do |i| + i += 1 + + if i % 5 == 0 && i % 3 == 0 + str.push('FizzBuzz') + elsif i % 3 == 0 + str.push('Fizz') + elsif i % 5 == 0 + str.push('Buzz') + else + str.push(i.to_s) + end + end + + str +end + +n = 15 +fizz_buzz(n) +# => [ +# "1", +# "2", +# "Fizz", +# "4", +# "Buzz", +# "Fizz", +# "7", +# "8", +# "Fizz", +# "Buzz", +# "11", +# "Fizz", +# "13", +# "14", +# "FizzBuzz" +# ]