Fibonacci

# WARNING: this program assumes the
# fibonacci sequence starts at 1
def fib(num):
	"""return the number at index `num` in the fibonacci sequence"""
    if num <= 2:
        return 1
    return fib(num - 1) + fib(num - 2)

# method 2: use `for` loop
def fib2(num):
	a, b = 1, 1
	for _ in range(num - 1):
		a, b = b, a + b
	return a


print(fib(6))  # 8
print(fib2(6))  # same result, but much faster

0
10
U2647 115 points

                                                             fib(5)   
                     /                  
               fib(4)                fib(3)   
             /                      /     
         fib(3)      fib(2)         fib(2)    fib(1)
        /             /           /      
  fib(2)   fib(1)  fib(1) fib(0) fib(1) fib(0)
  /    
fib(1) fib(0)

0
0
4.33
3
Tomas 110 points

                                    f(n) = f(n-1) + f(n-2) 
                                  f(6)
                                   ^
  			                       /\
                f(5)               +                       f(4)
                ^
               /\                   +                        /\
                
        f(4)    +           f(3)                     f(3)    +    f(2)
       ^                       ^                     ^              ^
      /\                       /\                    /\            /\
   
 f(3)   +       f(2)            f(2) +f(1)       f(2) + f(1)   f(1) +  f(0)             
   ^              ^                ^                ^
   /\             /\                /\              /\
    
f(2) + f(1)      f(1) +  f(0)     f(1)+ f(0)       f(1) + f(0)         
  ^
  /\
f(1) +  f(0) 
  
//f(6) = 8   ==&gt;  f(1)*8    f(1) appears 8 times 
 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));  
  
f(1) == 1;   
  
  
  
  
  
  
  

4.33 (3 Votes)
0
0
0
Louise 115 points

                                    // program to generate fibonacci series up to n terms

// take input from the user
const number = parseInt(prompt('Enter the number of terms: '));
let n1 = 0, n2 = 1, nextTerm;

console.log('Fibonacci Series:');

for (let i = 1; i &lt;= number; i++) {
    console.log(n1);
    nextTerm = n1 + n2;
    n1 = n2;
    n2 = nextTerm;
}Copied

0
0
4.56
9
Ephemeralist 105 points

                                    # Easy fibonacci exercise
# Method #1
def fibonacci(n):
    # 1th: 0
    # 2th: 1
    # 3th: 1 ...
    if n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)

# Method #2
def fibonacci2(n):
    if n == 0: return 0
    n1 = 1
    n2 = 1
    # (1, n - 2) because start by 1, 2, 3... not 0, 1, 1, 2, 3....
    for i in range(1, n - 2):
        n1 += n2
        n2 = n1 - n2
    return n1


print(fibonacci(13))
# return the nth element in the fibonacci sequence

4.56 (9 Votes)
0
3.89
9

                                    import java.util.Scanner;
public class Fibonacci 
{
    public static void main(String[] args) 
    {
        int n, a = 0, b = 0, c = 1;
        Scanner s = new Scanner(System.in);
        System.out.print(&quot;Enter value of n:&quot;);
        n = s.nextInt();
        System.out.print(&quot;Fibonacci Series:&quot;);
        for(int i = 1; i &lt;= n; i++)
        {
            a = b;
            b = c;
            c = a + b;
            System.out.print(a+&quot; &quot;);
        }
    }
}

3.89 (9 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source