I will post as a memorandum the questions that were asked in the inexperienced job change and the answers that I investigated and processed in my own way.
It's an answer that I gave out in my own way, so I'd be very happy if you could correct or explain it!
Create a function that outputs numbers from 1 to 100 in sequence. However, if the number to be output is a multiple of 3, "Fizz" is displayed, if the number to be output is a multiple of 5, "Buzz" is displayed, and if the number to be output is a multiple of 3 and a multiple of 5, "Fizz" is displayed. Please output "FizzBuzz" instead of numbers. Also, output the line feed code (LF-\ n) after the output of each number or character string.
fizzbuzz code
(1..100).each do |n| #Repeat the process from 1 to 100
def fizzbuzz(n)
if n % 15 == 0 #Output FizzBuzz when it is a multiple of 3 and 5 (3✖︎5)(9/21 Addendum There is a better code fix in the comments.)
puts "FizzBuzz"
elsif n % 3 == 0 #Output Fizz when it is a multiple of 3
puts "Fizz"
elsif n % 5 == 0 #Output Buzz when it is a multiple of 5
puts "Buzz"
else
puts n #Output numbers if the above conditions are not met
end
end
puts fizzbuzz(n) #Output fizzbuzz method(9/21 Addendum No need for puts in comments)
end
Create a function that outputs prime numbers from 1 to 1000. The definition of a prime number is as follows.
A prime number is a natural number greater than 1 with a divisor of 1 and only itself.
Code that outputs prime numbers
primeReturn = [] #Make primeReturn an array
(1..1000).each do |n| #Repeat processing from 1 to 1000
next if n == 1 #If the if condition is true, move to the next process with next
if n == 2
primeReturn.push(n) #Add n to the end of the primeReturn array with the push method
next #Move on to the next process
end
#As pointed out in the comments, the algorithm here is a waste of man-hours.
judge = true
primeReturn.each do |number|
if n % number == 0 #Processing of prime numbers
judge = false
break #Forcibly end the current each process and move to the next process
end
end
primeReturn.push(n) if judge #Add n to the end of the primeReturn array with the push method only if judge is true
end
puts primeReturn #Output primeReturn (output prime number)
__ * In the case of ruby, it can be done more easily. __
simple code for ruby
require 'prime' #load prime library
Prime.each(1000) {|x| p x} #Oh, it's very easy. .. ..
I created a program that outputs numbers from 0 to 9999 in order of 1 step. How many times is the number 7 output when this program is executed? For example, if 7777 is output, the number 7 is counted as being output 4 times. Please answer the answer to this question, or the program to calculate the answer.
Code to count the number 7
num = [*(1..9999)].to_s # to_Convert the contents of the s array to a string
puts num.count("7") #Count the number of 7 characters from the character string in the array
https://qiita.com/motoki4917/items/ffc89d955e20b91d1014 https://techacademy.jp/magazine/7507 https://docs.ruby-lang.org/ja/latest/class/Prime.html https://docs.ruby-lang.org/ja/latest/method/Array/i/inspect.html https://qiita.com/syo19961113/items/9f189424b5af5e084d33