I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)

Introduction

It is an experience that the low-educated writer got into a situation where the problem of the tribonacci sequence had to be solved within 10 minutes and solved it with ruby within 10 minutes from the state where he forgot the word tribonatch (memory loss state). I will. For those who are confident in math or not, why not try it in less than 10 minutes?

problem

Question 1 1,3,7,11,21,39... What is the 50th number?

Now we have to solve the problem ...

... (; ゚ д ゚) Gollum

I lost consciousness for about 10 seconds, but I have to understand the current situation for the time being! !! I regained my spirit.

About the Tribonacci sequence

The Tribonacci sequence is a sequence in which the numbers in the terms are the sum of the numbers in the previous three terms ... It seems that you will study at the junior high school entrance exam, but what did you eat yesterday? My memory loss, which has only a level of memory, did not heal (... now 30 years old)

Search results ...

What is the number of tribonatches? ... Omitted a[0]=a[1]=0 a[2]=1 a[n]=a[n-1]+a[n-2]+a[n-3] Sequence defined as Find the general term of this sequence. f(z)=Σ[n=0→∞]a[n]zⁿ =a[0]+a[1]z+Σ[n=2→∞]a[n]zⁿ =Σ[n=2→∞]a[n]zⁿ =a[2]z²+Σ[n=3→∞]a[n]zⁿ =z²+Σ[n=3→∞]{a[n-1]+a[n-2]+a[n-3]}zⁿ =z²+Σ[n=3→∞]{a[n-1]}zⁿ+Σ[n=3→∞]{a[n-2]}zⁿ+Σ[n=3→∞]{a[n-3]}zⁿ =z²+zΣ[n=3→∞]{a[n-1]}z^(n-1)+z²Σ[n=3→∞]{a[n-2]}z^(n-2)+z³Σ[n=3→∞]{a[n-3]}z^(n-3) = z² + zΣ [n = 2 → ∞] a [n] zⁿ + z²Σ [n = 1 → ∞] a [n] zⁿ + z³Σ [n = 0 → ∞] a [n] zⁿ ...

~~ No, I can't understand it in 10 minutes! !! ~~ ~~ I gave up understanding ~~ and expressed it in ruby based on my interpretation.

Writer's interpretation

1st + 2nd + 3rd = 4th 2nd + 3rd + 4th = 5th repeat Tokoroten-style guy ← completely different

The first is expressed as a, the second is expressed as b, the third is expressed as c, and the total of the fourth is expressed as d. After adding, think that b becomes a, c becomes b, and d becomes c, substitute each, and repeat it 47 times! !!

tribonacci.rb


a = 1
b = 3
c = 7
n = 0
while n < 47
  d = a + b + c
  a = b
  b = c
  c = d
  n += 1
end

puts c

The last c is the 47th number! !!

This is it! !!
Express this idea a little more carefully ...

tribonacci.rb


puts "Please enter the number you want"
puts "First number"
a = gets.to_i
puts "Second number"
b = gets.to_i
puts "Third number"
c = gets.to_i
puts "What number do you want?"
t = gets.to_i

n = 0
while n < (t - 3)
  d = a + b + c
  a = b
  b = c
  c = d
  n += 1
end

puts "#{t}The second number is#{c}is"

You have now obtained the 50th number 17079382868243! !! I haven't measured the exact time, but I managed to implement it within 10 minutes.

in conclusion

I think there is a better calculation method, but for me with a low degree of education, this was the limit of what I could express within 10 minutes. How was everyone? You're pretty impatient within 10 minutes, right? There was a code written in about 5 lines on Wikipedia, so it should be easier to call, but I was tempered by the time limit and it ended up like this ... In order to become an engineer, I realized that I had to study mathematics as well as write code.

Recommended Posts

I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
I tried a calendar problem in Ruby
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
I tried to solve the problem of Google Tech Dev Guide
I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
I tried to make full use of the CPU core in Ruby
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
I tried to organize the session in Rails
I want to get the value in Ruby
I tried to organize the cases used in programming
[Java] I tried to solve Paiza's B rank problem
I tried to implement the Euclidean algorithm in Java
I tried the FizzBuzz problem
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I tried to summarize the basic grammar of Ruby briefly
I didn't understand the topological sort, so I looked it up and implemented it in BFS, and then tried to solve the AtCoder problem.
I tried to explain the method
I tried to summarize the words that I often see in docker-compose.yml
I tried to illuminate the Christmas tree in a life game
I tried to sort the data in descending order, ascending order / Rails
I tried to write code like a type declaration in Ruby
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
I tried to make Numeron which is not good in Ruby
I want to change the value of Attribute in Selenium of Ruby
[Ruby] I want to output only the odd-numbered characters in the character string
I tried to make a parent class of a value object in Ruby
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
I tried to summarize the methods used
I tried the new era in Java
[Ruby] I tried to diet the if statement code with the ternary operator
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I thought about the best way to create a ValueObject in Ruby
I tried the AutoValue library in Intellij
I tried to build Ruby 3.0.0 from source
I tried to make a sample program using the problem of database specialist in Domain Driven Design
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
I tried to get the distance from the address string to the nearest station with ruby
I tried using Docker for the first time
I tried to implement polymorphic related in Nogizaka.
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I want to use arrow notation in Ruby
I tried to implement deep learning in Java
[Competition Pro] Solve the knapsack problem with Ruby
[Ruby basics] I tried to learn modules (Chapter 1)
I tried touching Docker for the first time
I tried to output multiplication table in Java
I tried to set tomcat to run the Servlet.
I tried to build Micra mackerel in 1 hour!
How to solve an Expression Problem in Java
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
How to build the simplest blockchain in Ruby
[Java] Try to solve the Fizz Buzz problem
I tried to create a log reproduction script at the time of apt install