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 tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried to solve the problem of "multi-stage selection" with Ruby
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with 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 solve the Ruby bonus drink problem (there is an example of the answer)
I tried to organize the session in Rails
[Ruby] I tried to diet the if statement code with the ternary operator
[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 get the distance from the address string to the nearest station with ruby
I tried to organize the cases used in programming
[Java] I tried to solve Paiza's B rank problem
[At Coder] Solve the ABC183 D problem with Ruby
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[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
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 tried to automate LibreOffice Calc with Ruby + PyCall.rb (Ubuntu 18.04)
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
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 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 build the environment of PlantUML Server with Docker
[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 create an API to get data from a spreadsheet in Ruby (with service account)
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
I tried to build Ruby 3.0.0 from source
I tried to implement ModanShogi with Kinx
[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 running the Angular sample in Auth0 Quick Start with Docker
I tried to build a Firebase application development environment with Docker in 2020
I thought about the best way to create a ValueObject 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