fibonacci

Solutions on MaxInterview for fibonacci by the best coders in the world

showing results for - "fibonacci"
Lenny
30 Oct 2017
1# WARNING: this program assumes the
2# fibonacci sequence starts at 1
3def fib(num):
4	"""return the number at index `num` in the fibonacci sequence"""
5    if num <= 2:
6        return 1
7    return fib(num - 1) + fib(num - 2)
8
9# method 2: use `for` loop
10def fib2(num):
11	a, b = 1, 1
12	for _ in range(num - 1):
13		a, b = b, a + b
14	return a
15
16
17print(fib(6))  # 8
18print(fib2(6))  # same result, but much faster
Lenora
26 Aug 2019
1# Easy fibonacci exercise
2# Method #1
3def fibonacci(n):
4    # 1th: 0
5    # 2th: 1
6    # 3th: 1 ...
7    if n == 1:
8        return 0
9    elif n == 2:
10        return 1
11    else:
12        return fibonacci(n - 1) + fibonacci(n - 2)
13
14# Method #2
15def fibonacci2(n):
16    if n == 0: return 0
17    n1 = 1
18    n2 = 1
19    # (1, n - 2) because start by 1, 2, 3... not 0, 1, 1, 2, 3....
20    for i in range(1, n - 2):
21        n1 += n2
22        n2 = n1 - n2
23    return n1
24
25
26print(fibonacci(13))
27# return the nth element in the fibonacci sequence
María Camila
27 Nov 2019
1f(n) = f(n-1) + f(n-2) 
2                                  f(6)
3                                   ^
4  			                       /\
5                f(5)               +                       f(4)
6                ^
7               /\                   +                        /\
8                
9        f(4)    +           f(3)                     f(3)    +    f(2)
10       ^                       ^                     ^              ^
11      /\                       /\                    /\            /\
12   
13 f(3)   +       f(2)            f(2) +f(1)       f(2) + f(1)   f(1) +  f(0)             
14   ^              ^                ^                ^
15   /\             /\                /\              /\
16    
17f(2) + f(1)      f(1) +  f(0)     f(1)+ f(0)       f(1) + f(0)         
18  ^
19  /\
20f(1) +  f(0) 
21  
22//f(6) = 8   ==>  f(1)*8    f(1) appears 8 times 
23 double feb  = (1/Math.pow(5,0.5)) * (Math.pow((1+Math.pow(5,0.5))/2,n)) - (1/Math.pow(5,0.5))* (Math.pow((1-Math.pow(5,0.5))/2,n));  
24  
25f(1) == 1;   
26  
27  
28  
29  
30  
31  
32  
Nesrine
03 Nov 2016
1import java.util.Scanner;
2public class Fibonacci 
3{
4    public static void main(String[] args) 
5    {
6        int n, a = 0, b = 0, c = 1;
7        Scanner s = new Scanner(System.in);
8        System.out.print("Enter value of n:");
9        n = s.nextInt();
10        System.out.print("Fibonacci Series:");
11        for(int i = 1; i <= n; i++)
12        {
13            a = b;
14            b = c;
15            c = a + b;
16            System.out.print(a+" ");
17        }
18    }
19}
Iker
12 Apr 2016
1// program to generate fibonacci series up to n terms
2
3// take input from the user
4const number = parseInt(prompt('Enter the number of terms: '));
5let n1 = 0, n2 = 1, nextTerm;
6
7console.log('Fibonacci Series:');
8
9for (let i = 1; i <= number; i++) {
10    console.log(n1);
11    nextTerm = n1 + n2;
12    n1 = n2;
13    n2 = nextTerm;
14}Copied
Louisa
19 Aug 2019
1Each test case will contains a single integer n where n >=1.
2
Paolo
06 Sep 2019
1                         fib(5)   
2                     /                  
3               fib(4)                fib(3)   
4             /                      /     
5         fib(3)      fib(2)         fib(2)    fib(1)
6        /             /           /      
7  fib(2)   fib(1)  fib(1) fib(0) fib(1) fib(0)
8  /    
9fib(1) fib(0)
10
queries leading to this page
code fibonacci pythonfibonacci sequence pythonfibonacci sequence python codedefinition of fibonaccifibonacci input pythonfibonacci number program in pythonfibonacci python and return define fibonaccitake the fibonnocci numbers into a list in pythonfibonacci formula pythonfibonacci code in pythonfibonnacci in pythonfibonaci program in pythonhow to calculate fibonacci using pythonfib in pythonfibonacci sfibonacci no python function to generate fibonacci sequencefibonacci sequence formulafibonaccis number using pythonfibonacci list pythonfibonacci function pythonfibonacci program pythonwhat is fibonaccisuccessi c3 b3 de fibonaccifibonacci folgefibonacci sequence coding pythonpython fibonacci problem with expkanationwhat is fibonacci functionpython functions to print fibonaccihow to code the fibonacci sequence in pythonfibonachi codefibonacci method in pythonfibonacci sequence pythinfibonacci series dynamic programming pythonpython fibonacci sequence explainedfibonnaci sequence fibonacci sequence pyfibonacci numbers fibonnacci pythonfibonacci numbers and pythonfibonacci statementfibonacci nuperhow to do fibonacci sequence in pythonfibonacci program in python 3the fibonaccifibonacci code for pythonpython fibonacci functionhow to program fibnacci equence in python5 ways of fibonacci in pythonhow to fibonacci in pythonfibonacci code pyhonfibonacci cached pythonfebno series in pythonsuite de fibonaccifibonacci sequence pytohnfibonaci pythonfibonnaci sequence pythonfunction that returns fibonacci sequence in pythonfibonacci string pythonfibonacci valuehow to make fibonacci sequence in pythoncalculate fibonacci sequence pythonfibonaaci in pythonwhat was fibonacci python fibonacci pythonfibonacci python programfibbonaci pythonpython get fibonacci seriesfibonacci in pythonfibonacci series for loop python fibonacci numbers pythonfibo pythonfibonacci numbersfibonacci in pythonwrite a python code for fiboncci numberspython fibonacci examplecalculate fibonacci number pythonwhat is fibonacci number in pythonfibonacci of numbers in pythonfobonacci in pythonfibonacii in pythonfind fibonacci number with user input in pythonhow to make a fibonacci sequence in pythonfibonacci numbers in pythonpython print fibonacci sequencefibonachi numbersfibonacci sequencefibonacci em pythonfibonnaci sequence pythofibonacci algorithm pythonsolve fibonacci sequence using pythonfibonacci no pythonpython function to generate fibonacci 50 sequencefibonaccipython fibofibonacci 2b pythonhow to generate fibonacci numbers in pythonhow to write fibonacci sequence in pythonfibonacci sequence from given number pythonprint each value of the fibonacci sequence in pythonfibonacci what is itfibonacci number in pythonfibonacci calculator onlinepython fibonacii codepython create fibonacci sequencehow to print a fibonacci series in python below a numberpython fibbonacci sequencecoding fibonacci sequence python big ohow to get a list of fibonacci numbers in pythonfibonacci of a number in pythonfibonacii pythonis fibonacci in pythonfibonacci sequence python for loopbinoacci sequence pythonfibonacci numbers pythonfibonacci sequence python programizewhat does the fibonacci function in python do 3ffibonnaci pythonfibonacci nuberonline fibonaccipython fibomnacy python fibonacci numbersfibonacci graph pythonfibonacii codefibonacci nedirfibonacci series code in python forloopis fibonacci pythonfibonacci logic in pythonfibonacci series python codefibonacci sequence generator pythonfibonacci list pytonwhat is a fibonaccifibonacci function in pythonsolving fibonacci sequence in pythongenerate fibonacci sequence in pythonpython fibfibonacci like sequencecreating a fibonacci sequence in pythonprogram for fibonacci series in python using for loopfibonacci ipython fibomnacy functionpython 3 fibonacci sequencefibonacci sequence pyhonfibonacci conihow to print the fibonocci series in pythonfibonacci series in pythonpython generate fibonacci seriesfibonacci serieshow to do a fibonacci sequence in pythonfibonacci coding compression pythonfibonacci sequence to double initial valuekbonacci sequence pythonfibonacci nowrite function in python to calculate fibonacci series till given numberwhat is the fibonacci fibonacci sequence python recursionis fibonaccifibonacci sequence in pythonprint fibonacci series up to given number pythonfibonacci python codecompute fibonacci sequence in python modulefibonacci python with returnfibonacci scalperfibbonacci sequence in pythonpython fibonacci numberfibonacci sequence formula pythonpython generate fibonacci sequencepython code to guess fibonacci numberpython fibonacci sequenceis fibonacci number pythonfibonacci sequence python simplehow to enter fibonacci sequence in loop in pythonpython fibonacci 3fibonacci using pythonpython finbonacci sequencehow much is fibonacci numberfibonacci sequence function pythonwhatis fibonaccifunction that returns the fibonacci sequence in pythonfibbinaci sequenceserie de fibonacci pythonpython 2 fibonaccifibonacci sequencyfibonacci in list pythonprint fibonacci sequence pythonfibonacci expression pythonwho is fibonaccifibonacci with pythonpython code fibonacci sequencefibonacci numbers algorithm pythonpython print the fibonacci sequencehow to find the fibonacci sequence in pythonpython program to print the fibonacci sequencefibonacci number pythonpython fibonacifibonacci ownerprint fibonacci series in pythonhow to make the fibonacci sequence in pythonfibonacci program in python using functionwrite python script for fibonacci serieshow to fibonacci sequence in pythonfibonacci en pythonhow to print fibonacci numbers in pythonpython fibonacci programfibonacci sequence python stratingfibonacci pythonfibonacci sequence pythonpython fibonaccifibonacci python examplefibonacci code pythonfibonscci in pythonfibonacci numbers function python printpython fibonacci series using for loopfibonacci onlinefibonacci program in pythonbuilt in fibonacci pythonfibonacci spiralfibonacci codepython formula for fibonacci sequencefibbonaci sequence pythonfibonacci series using for loop in pythonnumber fibonacciwhat is the fibonacci sequence pythonfibonacci python3python fibonacci number programfibbonacci sequence pythonfibonacchi codewrite for loop for fibonacci series pythonlist fibonacci numbers pythonfibonacci python functionfibonacci secuenciahow to find number is fibonacchi sequence in pythonhow to print the fibonacci sequence in pythonfibonaci function on pythonfibonacci sequence code pythonfibonacci numbers using pythonfibonai pythonfibonacci sequence in pythfibonacci no in pythonfirst 50 fibonacci numbers in pythonwhat is fibonacci sequence in pythonfibonacci sequence pthonfibonacci reihefibonacci python simplefibonacci numberfibonacci sequence logic pythonfibonacci python using functionis the a python function for fibonacci numbersfibonanci in pythonfibonacchi with pythonfibonacci