I tried to solve the tribonacci sequence problem in Ruby, with recursion.

After seeing the article "I tried to solve the tribonacci sequence problem with Ruby (time limit 10 minutes)", I thought I wanted to write it recursively. So Kakiko ... is a lie, and I didn't really know what Tribonacci was, so I wrote it in a hurry.

code

# Tribonacci
def tribonacci n
  if n == 1
    return 1
  elsif n == 2
    return 1
  elsif n == 3
    return 2
  else
    return tribonacci(n - 3) + tribonacci(n - 2) + tribonacci(n - 1)
  end
end

p (1..10).map{|n| tribonacci n } # => [1, 1, 2, 4, 7, 13, 24, 44, 81, 149]

# Unit Test
require 'test/unit'

class TC_Foo < Test::Unit::TestCase
  def test_one
    assert_equal tribonacci(1), 1
  end

  def test_two
    assert_equal tribonacci(2), 1
  end

  def test_three
    assert_equal tribonacci(3), 2
  end

  def test_ten
    assert_equal tribonacci(10), 149
  end
end

in conclusion

What about tail call optimization? Or memoization? Or, write in a loop instead of recursion in the first place! Write in Haskell or Elm because it's a great opportunity! , If you write with an iterator? Ignore such an inner voice. Because I'm not confident of doing them in 10 minutes!

[PR] ** Praise HP12c **

Fibonacci, Tribonacci, Tetranatch with Ruby's repeat function!

Recommended Posts

I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to solve the problem of "multi-stage selection" with Ruby
I tried a calendar problem in Ruby
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 karaoke machine problem (there is an example of the answer)
I tried to organize the session in Rails
I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
[Competition Pro] Solve the knapsack problem with Ruby
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 want to get the value in Ruby
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
[At Coder] Solve the ABC183 D problem with Ruby
[At Coder] Solve the ABC182 D problem with Ruby
I tried to implement the Euclidean algorithm in Java
I tried DI with Ruby
I tried the FizzBuzz problem
I tried to increase the processing speed with spiritual engineering
I tried to summarize the basic grammar of Ruby briefly
I tried to interact with Java
I tried to explain the method
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 illuminate the Christmas tree in a life game
I tried to sort the data in descending order, ascending order / Rails
I tried to build the environment of PlantUML Server with Docker
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 implement the image preview function with Rails / jQuery
I tried to reimplement Ruby Float (arg, exception: true) with builtin
I tried to check the operation of gRPC server with grpcurl
[Java] I want to perform distinct with the key in the object
I tried to make Numeron which is not good in Ruby
I want to change the value of Attribute in Selenium of Ruby
I tried to summarize the methods used
I tried to get started with WebAssembly
I tried the new era in Java
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried the AutoValue library in Intellij
[Beginner] Let's solve AtCoder problem with Ruby while looking at the article!
[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 visualize the access of Lambda → Athena with AWS X-Ray
I was addicted to unit testing with the buffer operator in RxJava
I tried to measure and compare the speed of GraalVM with JMH
I tried to make a sample program using the problem of database specialist in Domain Driven Design
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to implement polymorphic related in Nogizaka.
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
I tried to manage struts configuration with Coggle
Since the du command used when the capacity is full is difficult to use, I tried wrapping it with ruby
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to compare the infrastructure technology of engineers these days with cooking.
I made blackjack with Ruby (I tried using minitest)
[Ruby basics] I tried to learn modules (Chapter 1)
I tried to output multiplication table in Java