Skip to content

Johnny2136/Fibonacci-in-Ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

Shields GitHub starsGitHub forksGitHub issuesTwitterGitHub testGitHub release (latest SemVer)

Fibonacci-in-ruby

Test repository

Originally used:

def fib (n)
  return 0 if n == 0

  a = 0
  b = 1

  (1..n).each do
    c = (a + b)
    a = b
    b = c
  end

  return b
end

puts (0..100 ).map { |n| fib(n) }

I also tried the cache method which works faster.

def fib(n, cache)
  return 1 if n <= 2
  return cache[n] if cache[n]
  cache[n] = fib(n - 1, cache) + fib(n - 2, cache)
end

cache = {}
p (1..100).map {|i| fib(i, cache)}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages