Explanation of the FizzBuzz problem

FizzBuzz problem commentary

I also summarized Expression used for FizzBuzz problem.

** Step 1 ** Output numbers from 1 to 100 to the terminal

** Step 2 ** When it is "multiple of 3", Fizz is a character string instead of a number. Similarly for "multiples of 5", Buzz

** Step 3 ** Output as FizzBuzz when it is a multiple of 3 and 5, which is a multiple of 15.


Program template

Template


def fizz_buzz 
#Contents of processing
end

fizz_buzz

A program to output numbers from 1 to 100 to the terminal

step 1 Output numbers from 1 to 100 to the terminal

Output numbers from 1 to 100 to the terminal


def fizz_buzz
  num = 1
# num =Contents of processing starting from 1
  while num <= 100 do #As long as the boolean value is true, the following processing continues
    puts num #Output numerical value

    num = num + 1 #To boolean value+1
  end
#Contents of processing
end

fizz_buzz

When it is "multiple of 3", it outputs Fizz as a character string, and when it is "multiple of 5", it outputs Buzz.

** Step 2 ** When it is a multiple of 3, use a character string instead of a number. Buzz` for" multiples of 5 "

When it is "multiple of 3", it is output as Fizz as a character string, and when it is "multiple of 5", it is output as Buzz.


def fizz_buzz
  num = 1
# num =Contents of processing starting from 1
  while num <= 100 do #As long as the boolean value is true, the following processing continues
    if num % 3 == 0 #When the remainder divided by 3 is 0
      puts "Fizz"
    elsif num % 5 == 0 #When the remainder after dividing by 5 is 0
      puts "Buzz"
    else #At other times
      puts num #Numerical values ​​that do not correspond to the above multiples of 3 and 5 are output as they are.
    end
    num = num + 1 #To boolean value+1
  end
#Contents of processing
end

fizz_buzz

Add a conditional expression for "multiples of 15 (multiples of 3 and 5)"

** Step 3 ** FizzBuzz and output when it is a multiple of 3 and 5, which is a multiple of 15.

Added conditional expression for "multiples of 15 (multiples of 3 and 5)"


def fizz_buzz
  num = 1
# num =Contents of processing starting from 1
  while (num <= 100) do #As long as the boolean value is true, the following processing continues
    if num % 15 == 0 #When it is a multiple of 15
      puts "FizzBuzz"
    elsif (num % 3) == 0 #When the remainder divided by 3 is 0
      puts "Fizz"
    elsif (num % 5) == 0 #When the remainder after dividing by 5 is 0
      puts "Buzz"
    else #At other times
      puts num #Numerical values ​​that do not correspond to multiples of 3, 5, and 15 above are output as they are.
    end
    num = num + 1 #To boolean value+1
  end
#Contents of processing
end

fizz_buzz

Notes If you add it below, the above conditional expression "is it a multiple of 3" or "is it a multiple of 5" becomes true and is displayed as Fizz or Buzz.

From the above points, add a condition to output FizzBuzz when it is a "multiple of 15" at the beginning of the if statement.

Recommended Posts

Explanation of the FizzBuzz problem
A memorandum of the FizzBuzz problem
Let's solve the FizzBuzz problem!
I tried the FizzBuzz problem
FizzBuzz problem
Explanation of the order of rails routes
Expression used in the fizz_buzz problem
[Ruby] FizzBuzz problem
[Ruby] FizzBuzz problem
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (1)
The world of clara-rules (3)
The world of clara-rules (5)
The idea of quicksort
jMetal Explanation --Problem interface
The idea of jQuery
IOTA: [Technical explanation] The whole picture of the remittance bundle.
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
About the handling of Null
A quick explanation of the five types of static in Java
Docker monitoring-explaining the basics of basics-
The play of instantiating java.lang.Void
The basics of Swift's TableView
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
Java: The problem of which is faster, stream or loop
About the explanation about functional type
Median of the three values
The illusion of object orientation
A brief explanation of commitAllowingStateLoss
Switch the version of bundler
I tried to solve the problem of "multi-stage selection" with Ruby
A solution to the problem of blank spaces at the beginning of textarea
Problem that the attribute of User model becomes nil in ActionMailer
I tried to solve the problem of Google Tech Dev Guide
About the behavior of ruby Hash # ==
[Java] Delete the elements of List
Continuation: The parable of OOP (omitted)
Understand the basic mechanism of log4j2.xml
About the basics of Android development
[Rails] Check the contents of the object
Replace the contents of the Jar file
[Java version] The story of serialization
Check the version of Cent OS
[Swift] Change the textColor of UIDatePicker
[Ruby] See the essence of ArgumentError
[Docker Network Chapter 2] Explanation of Docker Networking
I read the source of ArrayList I read
Note on the path of request.getRequestDispatcher
[Docker Network Chapter 1] Explanation of Docker Networking
The basic basis of Swift dialogs
The basic basis of Swift's Delegate
I read the source of Integer
This and that of the JDK
The story of @ViewScoped consuming memory
Filter the fluctuations of raw data
Explaining the columns of Spree :: Taxonomy
I read the source of Long
Official logo list of the service
Explain the column of Spree :: Product