I wonder if there is anything I can post to Qiita. I dug up the script when I tried Project Euler more than 6 years ago, so Write it (2nd)
The execution environment is as follows. ruby 2.6.3p62 macOS Catalina 10.15.6 zsh
・ If it is an even number, divide by 2. ・ For odd numbers, multiply by 3 and add 1. A sequence of numbers that goes like this. Just because the number is large does not mean that the sequence becomes long, which is strange. See below for a detailed and accurate explanation. https://ja.wikipedia.org/wiki/コラッツの問題
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
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 result will be as follows.
% 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]
A sequence in which an arbitrary number is the sum of the number before it and the number before it. Impression that it often appears as a program problem next to fizz and buzz. See below for a detailed and accurate explanation. https://ja.wikipedia.org/wiki/フィボナッチ数
fibonacci_sequence.rb
def create_fibonacci_sequence( term1, term2, max_term)
fibo_ary = Array.new
fibo_ary.push term1
fibo_ary.push term2
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
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]
It is a sequence that increases as +2, +3, +4, +5, .... When it comes to the number of points, they are arranged in an equilateral triangle, so it seems to be called a triangular number. It's fashionable. See below for a detailed and accurate explanation. https://ja.wikipedia.org/wiki/三角数
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.
Recommended Posts