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?
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.
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.
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.
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