Questions tagged [fibonacci]

The Fibonacci sequence is the sequence defined by F(0) = 0, F(1) = 1, F(n + 2) = F(n) + F(n + 1). The first few terms are 0, 1, 1, 2, 3, 5, 8.

The Fibonacci sequence is the sequence defined by

F(0) = 0
F(1) = 1
F(n + 2) = F(n) + F(n + 1).  

The first few terms are 0, 1, 1, 2, 3, 5, and 8.


The most efficient way to compute the first N values is to iterate over an array using the above formula, resulting in O(N) operations (O(N²) if digit or bit operations are counted). The recursive implementation should be generally avoided, since it is O(φN) where φ is the golden ratio and is equal to (1+sqrt(5))/2. However, by using a cache for already computed values, it can be as fast as the iterative implementation.


One efficient method for computing single Fibonacci numbers is

Fib(n) = Round(  Power( 0.5*(1+Sqrt(5)), n ) / Sqrt(5)  );

The square root and power have to be computed in sufficient precision, roughly, Fib(n) requires about 0.2*n decimal digits or about 0.7*n bits in the integer result.


Another method is based on the fact that the matrix

( Fib[n+1] Fib[ n ] )
( Fib[ n ] Fib[n-1] )

is the n-th power of the matrix

( 1 1 ) which is ( Fib[2] Fib[1] )
( 1 0 ) equal to ( Fib[1] Fib[0] )

This is the basis of a halving-and-squaring method that computes Fib[N] in O(log(N)) operations, completely in (big) integer arithmetic.

If one accounts for the complexity of big integer multiplication, the complexity raises to O( M(N) ) digit or bit operations, where M(d) is the complexity of the multiplication of numbers of bit length d. Typical methods have M(d)=O(d*log(d)) for FFT based multiplication, M(d)=O(dw) with w=log2(3) for Karatsuba and smaller w for the Toom-Cook methods.

2345 questions
0
votes
3 answers

How can I change my reduce() to sum numbers until I get the number which was asked for?

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers I need to complete theses task, so I have made a function to sum Fibonacci series. But I need to sum until I…
0
votes
2 answers

Why am I getting fibonacci numbers up to the size of my array?

I want to print the fibonacci series below 1000. But in my below code, I don't know why I am getting the fibonacci numbers up to the maximum size of my defined array? int main(){ int dp[22] = {0}; dp[0] = 0, dp[1] = 1; count<
0
votes
1 answer

Python: write fibonacci coroutine

I want to write a fibonacci function that behaves as a coroutine in python. Here is basically what I want to accomplish: def fibonacci(limit: int) -> int: ... fib = fibonacci(limit=100_000_000) next(fib) fib.send(10) # -> 55 next(fib) # ->…
Vladimir Yahello
  • 243
  • 1
  • 2
  • 10
0
votes
2 answers

how to save all terms in Fibonacci sequence till `n`

I was trying to save all items in Fibonacci sequence till a given number n. For example, if the function is fib, my expected out would be 1 1 2 for fib(3) 1 1 2 3 5 for fib(5) 1 1 2 3 5 8 13 21 for fib(8) so on and so forth. My code is fib <-…
user13057229
0
votes
2 answers

Could anyone please explain how the last line of the code is working. The language used in python, I am new to python?

I am not able to understand how the last line is able to print the Fibonacci series. It will be a great help, if someone can sequentially explain the code. a , b = 0 , 1 while b < 10: print(b, end = ' ') a, b = b, a + b
Suel Ahmed
  • 63
  • 5
0
votes
0 answers

CSS: 100% responvie rectangle without scrolling

This code makes the square fully responsive. http://jsfiddle.net/9ow3ymfz/ How can I reach the same goal with a rectangle? I'm trying to use CSS to make the rectangle fully visible if the screen height is less than the height of the rectangle…
Dmitry
  • 29
  • 6
0
votes
0 answers

How does multiple assignment in Fibonacci code used in Python website work?

I'm a newbie to Python and just can't understand this: Code#1: a, b = 0, 1 while a < 10: print(a) a, b = b, a+b Code#2 a, b = 0, 1 while a < 10: print(a) a = b b = a+b Why Code#1 produce Fibonacci sequence while Code#2 doesn't?
reza moradi
  • 17
  • 1
  • 5
0
votes
0 answers

python recursion oop vs functional

I'm curious why this first function does work (OOP) but the second triggers and error. Can someone explain? class Solution(object): def __init__(self): self.solved = {0:0,1:1,2:1} def fibonacci(self, n): """ :type n:…
mjake
  • 43
  • 1
  • 5
0
votes
2 answers

JAVA: Fibonacci Recursive and Non-Recursive Function

Hi there sorry for this noob question. I'm Mario and may I ask if my program is correct for recursive and non-recursive function for Fibonacci Secquence nth Value. static int recursiveMethod(int num) { if (num <= 1) return num; return…
0
votes
3 answers

Recursion function in two types for Fibonacci sequence

This is the original code for Fibonacci sequence by using Recursion def rec(n): if n<=1: return n else: return ( rec(n-1) + rec(n-2)) n=int(input()) Above code gets very slow in around 50th term. Following code I have returned is also…
user11091815
0
votes
1 answer

C++ program to find the closest number in the Fibonacci sequence

I need to write a program in C++ to find the closest Fibonacci number to the input number. I patched together a program that tells the Fibonacci number in the nth place, I've included it's code. Next is this task of finding the closest number to the…
0
votes
1 answer

The value none is being returned Fibonacci sequence

I'm completely confused as to why my code is returning none. I'm new and have been searching for a while and i'm still lost. class Fibonacci: def __init__(self, max = 0): self.max = max def __iter__(self): self.n = 0 …
0
votes
1 answer

i have a pinescript need convert from version 2 to version 4

this is the original code for version 2 but after conversion the result totally different from the version 2. is there any mistake i make when do convert to version 4 //@version=2 study("Auto Pivots with S/R",overlay = true) // Inputs pivotType =…
ZKTON
  • 61
  • 1
  • 2
0
votes
1 answer

Fibonacci Function in MATLAB

I am attempting to create a function for the Fibonacci numbers using a for loop. My code is as follows: function fib = fibGenerator(N) fib(1) = 0; fib(2) = 1; for i = 3:N fib(i) = fib(i-1)+fib(i-2); end The following error message is displayed:…
0
votes
1 answer

Fibonacci Series in Octave

I need to run fibonacci series through function in Octave. I got the expected output, but my test case fails due to indendation in output. function fibo(n) a=0; b=1; x(:,1)=[1]; for i=2:n c=a+b; x(:,i)=[c]; a=b; b=c; endfor …
Raja
  • 71
  • 3
  • 11