Closed form Fibonacci Series
up vote
1
down vote
favorite
I am using Python to create a Fibonacci using this formula:
I have this recursive Fibonacci function:
def recursive_fibonacci(n):
if n <= 1:
return int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
else:
return(recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2))
To display it I am using this:
nterms = 10
if nterms <= 0:
print("Please Enter a positive integer")
else:
print("Recursive Fibonacci Sequence: " ,
[recursive_fibonacci(i) for i in range(nterms)])
print("Iterative Fibonacci Sequence: " ,
[iterative_fib(i) for i in range(nterms)])
How would I use an iterative function with this Fibonacci?
I've tried using this:
def iterative_fib(n):
equation = lambda n: int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
if n <= 1:
return equation(n)
else:
a, b = 1, 2
for i in range(n):
fn = equation((i-a)+(i-b))
return fn
However this iterative function doesn't seem to have the same output as the recursive function.
The output of the recursive function:
Recursive Fibonacci Sequence: [0, 2, 2, 4, 6, 10, 16, 26, 42, 68]
The output of the iterative function:
Iterative Fibonacci Sequence: [0, 2, 2, 2, 3, 6, 13, 27, 58, 122]
python recursion iteration fibonacci
add a comment |
up vote
1
down vote
favorite
I am using Python to create a Fibonacci using this formula:
I have this recursive Fibonacci function:
def recursive_fibonacci(n):
if n <= 1:
return int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
else:
return(recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2))
To display it I am using this:
nterms = 10
if nterms <= 0:
print("Please Enter a positive integer")
else:
print("Recursive Fibonacci Sequence: " ,
[recursive_fibonacci(i) for i in range(nterms)])
print("Iterative Fibonacci Sequence: " ,
[iterative_fib(i) for i in range(nterms)])
How would I use an iterative function with this Fibonacci?
I've tried using this:
def iterative_fib(n):
equation = lambda n: int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
if n <= 1:
return equation(n)
else:
a, b = 1, 2
for i in range(n):
fn = equation((i-a)+(i-b))
return fn
However this iterative function doesn't seem to have the same output as the recursive function.
The output of the recursive function:
Recursive Fibonacci Sequence: [0, 2, 2, 4, 6, 10, 16, 26, 42, 68]
The output of the iterative function:
Iterative Fibonacci Sequence: [0, 2, 2, 2, 3, 6, 13, 27, 58, 122]
python recursion iteration fibonacci
4
If you have a formula, why do you need either recursion or iteration?
– Scott Hunter
Nov 11 at 0:13
I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
– Sanchit Kumar
Nov 11 at 0:18
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am using Python to create a Fibonacci using this formula:
I have this recursive Fibonacci function:
def recursive_fibonacci(n):
if n <= 1:
return int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
else:
return(recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2))
To display it I am using this:
nterms = 10
if nterms <= 0:
print("Please Enter a positive integer")
else:
print("Recursive Fibonacci Sequence: " ,
[recursive_fibonacci(i) for i in range(nterms)])
print("Iterative Fibonacci Sequence: " ,
[iterative_fib(i) for i in range(nterms)])
How would I use an iterative function with this Fibonacci?
I've tried using this:
def iterative_fib(n):
equation = lambda n: int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
if n <= 1:
return equation(n)
else:
a, b = 1, 2
for i in range(n):
fn = equation((i-a)+(i-b))
return fn
However this iterative function doesn't seem to have the same output as the recursive function.
The output of the recursive function:
Recursive Fibonacci Sequence: [0, 2, 2, 4, 6, 10, 16, 26, 42, 68]
The output of the iterative function:
Iterative Fibonacci Sequence: [0, 2, 2, 2, 3, 6, 13, 27, 58, 122]
python recursion iteration fibonacci
I am using Python to create a Fibonacci using this formula:
I have this recursive Fibonacci function:
def recursive_fibonacci(n):
if n <= 1:
return int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
else:
return(recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2))
To display it I am using this:
nterms = 10
if nterms <= 0:
print("Please Enter a positive integer")
else:
print("Recursive Fibonacci Sequence: " ,
[recursive_fibonacci(i) for i in range(nterms)])
print("Iterative Fibonacci Sequence: " ,
[iterative_fib(i) for i in range(nterms)])
How would I use an iterative function with this Fibonacci?
I've tried using this:
def iterative_fib(n):
equation = lambda n: int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
if n <= 1:
return equation(n)
else:
a, b = 1, 2
for i in range(n):
fn = equation((i-a)+(i-b))
return fn
However this iterative function doesn't seem to have the same output as the recursive function.
The output of the recursive function:
Recursive Fibonacci Sequence: [0, 2, 2, 4, 6, 10, 16, 26, 42, 68]
The output of the iterative function:
Iterative Fibonacci Sequence: [0, 2, 2, 2, 3, 6, 13, 27, 58, 122]
python recursion iteration fibonacci
python recursion iteration fibonacci
edited Nov 11 at 1:01
coldspeed
111k17101170
111k17101170
asked Nov 11 at 0:02
Roman Roshchuk
406
406
4
If you have a formula, why do you need either recursion or iteration?
– Scott Hunter
Nov 11 at 0:13
I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
– Sanchit Kumar
Nov 11 at 0:18
add a comment |
4
If you have a formula, why do you need either recursion or iteration?
– Scott Hunter
Nov 11 at 0:13
I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
– Sanchit Kumar
Nov 11 at 0:18
4
4
If you have a formula, why do you need either recursion or iteration?
– Scott Hunter
Nov 11 at 0:13
If you have a formula, why do you need either recursion or iteration?
– Scott Hunter
Nov 11 at 0:13
I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
– Sanchit Kumar
Nov 11 at 0:18
I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
– Sanchit Kumar
Nov 11 at 0:18
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
The equation you're trying to implement is the closed form Fibonacci series.
Closed form means that evaluation is a constant time operation.
g = (1 + 5**.5) / 2 # Golden ratio.
def fib(N):
return int((g**N - (1-g)**N) / 5**.5)
Contrast with,
def fib_iterative(N):
a, b, i = 0, 1, 2
yield from (a, b)
while i < N:
a, b = b, a + b
yield b
i += 1
And we have
>>> [fib(n) for n in range(10)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> list(fib_iterative(10))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
add a comment |
up vote
1
down vote
I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.
Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.
Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.
Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.
To illustrate, when n = 3, f_3 is calculated as -
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The equation you're trying to implement is the closed form Fibonacci series.
Closed form means that evaluation is a constant time operation.
g = (1 + 5**.5) / 2 # Golden ratio.
def fib(N):
return int((g**N - (1-g)**N) / 5**.5)
Contrast with,
def fib_iterative(N):
a, b, i = 0, 1, 2
yield from (a, b)
while i < N:
a, b = b, a + b
yield b
i += 1
And we have
>>> [fib(n) for n in range(10)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> list(fib_iterative(10))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
add a comment |
up vote
2
down vote
accepted
The equation you're trying to implement is the closed form Fibonacci series.
Closed form means that evaluation is a constant time operation.
g = (1 + 5**.5) / 2 # Golden ratio.
def fib(N):
return int((g**N - (1-g)**N) / 5**.5)
Contrast with,
def fib_iterative(N):
a, b, i = 0, 1, 2
yield from (a, b)
while i < N:
a, b = b, a + b
yield b
i += 1
And we have
>>> [fib(n) for n in range(10)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> list(fib_iterative(10))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The equation you're trying to implement is the closed form Fibonacci series.
Closed form means that evaluation is a constant time operation.
g = (1 + 5**.5) / 2 # Golden ratio.
def fib(N):
return int((g**N - (1-g)**N) / 5**.5)
Contrast with,
def fib_iterative(N):
a, b, i = 0, 1, 2
yield from (a, b)
while i < N:
a, b = b, a + b
yield b
i += 1
And we have
>>> [fib(n) for n in range(10)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> list(fib_iterative(10))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
The equation you're trying to implement is the closed form Fibonacci series.
Closed form means that evaluation is a constant time operation.
g = (1 + 5**.5) / 2 # Golden ratio.
def fib(N):
return int((g**N - (1-g)**N) / 5**.5)
Contrast with,
def fib_iterative(N):
a, b, i = 0, 1, 2
yield from (a, b)
while i < N:
a, b = b, a + b
yield b
i += 1
And we have
>>> [fib(n) for n in range(10)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> list(fib_iterative(10))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
answered Nov 11 at 0:18
coldspeed
111k17101170
111k17101170
add a comment |
add a comment |
up vote
1
down vote
I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.
Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.
Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.
Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.
To illustrate, when n = 3, f_3 is calculated as -
add a comment |
up vote
1
down vote
I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.
Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.
Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.
Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.
To illustrate, when n = 3, f_3 is calculated as -
add a comment |
up vote
1
down vote
up vote
1
down vote
I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.
Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.
Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.
Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.
To illustrate, when n = 3, f_3 is calculated as -
I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.
Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.
Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.
Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.
To illustrate, when n = 3, f_3 is calculated as -
edited Nov 11 at 0:35
answered Nov 11 at 0:23
Shash
487
487
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244630%2fclosed-form-fibonacci-series%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
If you have a formula, why do you need either recursion or iteration?
– Scott Hunter
Nov 11 at 0:13
I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
– Sanchit Kumar
Nov 11 at 0:18