text: [Ruby] I made a program of various numbers such as Collats number, Fibonacci number, Triangle number
  I wonder if there is something that I can post to Qiita… I dug up the script when I tried Project Euler a little over 6 years ago, Write it (2nd stage)
  The execution environment is as follows. ruby 2.6.3p62 macOS Catalina 10.15.6 zsh
  Generate Collats number
  ・If the number is even, divide by two. ・If it is an odd number, multiply 3 and add 1. A sequence of numbers that proceeds like. Just because the number is large doesn’t mean that the sequence will be long, it’s strange. See below for detailed and accurate explanation. Collats issue
  collatz_number.rb
  def get_collatz_next_number(num) if num.even? then return num / 2 elsif num == 1 then return nil else return (num * 3) + 1 end end def create_collatz_sequence(num) cltz_ary = Array.new new_num = num cltz_ary.push new_num new_num = get_collatz_next_number(new_num) while !(new_num.nil?) do cltz_ary.push new_num new_num = get_collatz_next_number(new_num) end It's a sequel. return cltz_ary end p create_collatz_sequence (26) p "====" p create_collatz_sequence (27) p "====" p create_collatz_sequence (28) p "====" p create_collatz_sequence (1652)
  The following execution result will be obtained.
  % ruby collatz_number.rb [26, 13, 40, 20, 10, 5, 16, 8, 4,] "====" [28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] "====" [1652, 826, 413, 1240, 620,]
  Generate Fibonacci sequence
  A sequence in which any number is the sum of its previous number and its previous number. The impression that it often appears as a program problem after fizz and buzz. See below for detailed and accurate explanation. Fibonacci number
  fibonacci_sequence.rb
  def create_fibonacci_sequence( term1, term2, max_term) fibo_ary = Array.new fibo_ary.push term1 fibo_ary.push term2 It's a sequel. new_num = 0 i = 2 new_num = fibo_ary[i-1] + fibo_ary[i-2] while new_num <= max_term do fibo_ary.push new_num i += 1 new_num = fibo_ary[i-1] + fibo_ary[i-2] end It's a sequel. return fibo_ary end p create_fibonacci_sequence(0,1,10946)
  The execution result is as follows.
  % ruby fibonacci_sequence.rb [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]
  Generate triangle
  It is a sequence of numbers increasing from +2,+3,+4,+5,… It seems that it is called a triangle number because it is arranged in an equilateral triangle when it is the number of points. You’re fashionable. See below for detailed and accurate explanation. number
  triangle_number.rb
  def create_triangle_number (max_number) terms = Array.new i = 0 current_number = 1 while current_number <= max_number do terms.push current_number i += 1 current_number = terms[i-1] + i + 1 end return terms end p create_triangle_number (1000)
  The execution result is as follows.
  ruby triangle_number.rb ]
  that’s all.