Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,6 +1,6 @@
|
|||
def fibIter(n)
|
||||
def fib_iter(n)
|
||||
return 0 if n == 0
|
||||
fibPrev, fib = 1, 1
|
||||
(n.abs - 2).times { fibPrev, fib = fib, fib + fibPrev }
|
||||
fib * (n<0 ? (-1)**(n+1) : 1)
|
||||
fib_prev, fib = 1, 1
|
||||
(n.abs - 2).times { fib_prev, fib = fib, fib + fib_prev }
|
||||
fib * (n < 0 ? (-1)**(n + 1) : 1)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
def fibRec(n)
|
||||
def fib_rec(n)
|
||||
if n <= -2
|
||||
(-1)**(n+1) * fibRec(n.abs)
|
||||
(-1)**(n + 1) * fib_rec(n.abs)
|
||||
elsif n <= 1
|
||||
n.abs
|
||||
else
|
||||
fibRec(n-1) + fibRec(n-2)
|
||||
fib_rec(n - 1) + fib_rec(n - 2)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@
|
|||
|
||||
fib = Hash.new do |f, n|
|
||||
f[n] = if n <= -2
|
||||
(-1)**(n+1) * f[n.abs]
|
||||
(-1)**(n + 1) * f[n.abs]
|
||||
elsif n <= 1
|
||||
n.abs
|
||||
else
|
||||
f[n-1] + f[n-2]
|
||||
f[n - 1] + f[n - 2]
|
||||
end
|
||||
end
|
||||
# examples: fib[10] => 55, fib[-10] => (-55/1)
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ M = Matrix[[0, 1], [1,1]]
|
|||
# and then multiplying that by by M**(the remaining number of times). E.g., to compute
|
||||
# M**19, compute partial = ((M**2)**2) = M**16, and then compute partial*(M**3) = M**19.
|
||||
# That's only 5 matrix multiplications of M to compute M*19.
|
||||
def self.fibMatrix(n)
|
||||
def self.fib_matrix(n)
|
||||
return 0 if n <= 0 # F(0)
|
||||
return 1 if n == 1 # F(1)
|
||||
# To get F(n >= 2), compute M**(n - 1) and extract the lower right element.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
require 'generator'
|
||||
|
||||
def fibGen
|
||||
def fib_gen
|
||||
Generator.new do |g|
|
||||
f0, f1 = 0, 1
|
||||
loop do
|
||||
g.yield f0
|
||||
f0, f1 = f1, f0+f1
|
||||
f0, f1 = f1, f0 + f1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue