[RUBY] Collatz number, Fibonacci number, triangular number I tried to make various sequence programs

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

Generate Collatz numbers

・ 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]

Generate a Fibonacci sequence

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]

Generate a triangular number

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

Collatz number, Fibonacci number, triangular number I tried to make various sequence programs
Amicable numbers, perfect numbers, excess numbers, deficient numbers, palindromic numbers I tried to make various numbers of programs
I made various Fibonacci sequence functions (Ruby)
I tried to make Basic authentication with Java
I tried to make a login function in Java
I tried to make FizzBuzz that is uselessly flexible
I tried node-jt400 (Programs)
I tried to make an application in 3 months from inexperienced
I tried to make an introduction to PHP + MySQL with Docker
I tried to make Venn diagram an easy-to-understand GIF animation
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried to make Java Optional and guard clause coexist
I tried to make a client of RESAS-API in Java
I tried to verify yum-cron
[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
I tried to make an Android application with MVC now (Java)
I tried to make it an arbitrary URL using routing nesting
[Java] I tried to make a maze by the digging method ♪
I tried to make Numeron which is not good in Ruby
I tried to make a group function (bulletin board) with Rails
I tried to chew C # (indexer)
I tried to interact with Java
I tried to explain the method
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried to make a parent class of a value object in Ruby
I tried to make a simple face recognition Android application using OpenCV
[iOS] I tried to make a processing application like Instagram with Swift
I tried to make a Web API that connects to DB with Quarkus
I tried to make my own transfer guide using OpenTripPlanner and GTFS
I made a virtual currency arbitrage bot and tried to make money
I tried to make full use of the CPU core in Ruby
I tried to make a talk application in Java using AI "A3RT"
I introduced OpenAPI (Swagger) to Spring Boot (gradle) and tried various settings