[Ruby] I made a program of various numbers such as Collats number, Fibonacci number, Triangle number

3 minute read

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. https://ja.wikipedia.org/wiki/ 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, 2, 1]
"===="
[27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103 , 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132 , 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051 , 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106 , 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]
"===="
[28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
"===="
[1652, 826, 413, 1240, 620, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167 , 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822 , 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92 , 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]

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. https://ja.wikipedia.org/wiki/ 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. https://ja.wikipedia.org/wiki/Triangular 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
[1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325 , 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990]

that’s all.