I tried the FizzBuzz problem

The famous FizzBuzz problem

Outputs a numerical value from 1 to 100. At that time, it outputs Fizz when it is a multiple of 3, Buzz when it is a multiple of 5, and FizzBuzz when it is a multiple of 15.

Answer

Create a fizz_buzz method to solve this problem.

def fizz_buzz
  1.upto(100)  do |i|
    if i % 3 == 0 && i  % 5 == 0
      puts "FizzBuzz"
    elsif i % 3 == 0 && i % 5 != 0 
      puts "Fizz"
    elsif i % 3 != 0 && i % 5 == 0
      puts "Buzz"
    else
      puts i
    end
  end
end

Impressions

I felt that it was a problem that could dig deeper into the two perspectives (collective idea of mathematics and the specifications of the ruby program) of how to combine conditional expressions and how much the program reflects them.

Recommended Posts

I tried the FizzBuzz problem
I tried FizzBuzz.
Explanation of the FizzBuzz problem
I tried the Docker tutorial!
I tried the VueJS tutorial!
Let's solve the FizzBuzz problem!
I tried to explain the method
I tried the Java framework "Quarkus"
[Rails] I tried deleting the application
Expression used in the fizz_buzz problem
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
I tried to solve the problem of "multi-stage selection" with Ruby
[Ruby] FizzBuzz problem
I tried the new era in Java
[Ruby] FizzBuzz problem
I tried Spring.
I tried to implement the Iterator pattern
I tried youtubeDataApi.
I tried refactoring ①
I tried the AutoValue library in Intellij
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
I tried JHipster 5.1
I tried to solve the problem of Google Tech Dev Guide
I tried using Docker for the first time
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I tried touching Docker for the first time
I tried to set tomcat to run the Servlet.
I tried using the profiler of IntelliJ IDEA
I tried running Autoware
I tried using Gson
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried QUARKUS immediately
I tried using TestNG
I tried using Galasa
I tried node-jt400 (Programs)
I tried node-jt400 (execute)
I tried node-jt400 (Transactions)
I tried to organize the cases used in programming
I tried using the Server Push function of Servlet 4.0
[Java] I tried to solve Paiza's B rank problem
I tried to decorate the simple calendar a little
[Rails] I tried playing with the comment send button
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I tried using the Migration Toolkit for Application Binaries
I tried installing the Docker Integration plugin in IntelliJ
I tried to make FizzBuzz that is uselessly flexible
[WIP] I tried the configuration of Docker + Streama + NFS
I tried to implement the Euclidean algorithm in Java
I tried to implement the like function by asynchronous communication
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I investigated the enclosing instance.
I tried using the GitHub repository as a library server
I tried node-jt400 (Environment construction)
I tried DI with Ruby
I tried node-jt400 (IFS write)
Don't underestimate the N + 1 problem!
[JDBC] I tried to access the SQLite3 database from Java.
I summarized the collection framework.
I tried node-jt400 (SQL Update)