Generating Fibonacci Sequence

Generating the Fibonacci sequence programmatically

What is a Fibonacci Sequence
The Fibonacci sequence starts with the number 0 followed by 1. Each subsequent number is the addition of the previous two numbers. Thus the Fibonacci sequence looks like below
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on.


Algorithm for generating the Fibonacci sequence

1.   Fibonacci (N)
2.   If N <= 2
3.     for number from 0 upto N-1
4.     print number
5.   Else
6.     Initialize a = 0, b = 1
7.     print a, b
8.     While N is greater than 2 do
9.         next_number = a + b
10.         print next_number
11.         a = b
12.         b = next_number
13.         N = N-1



Fibonacci sequence implementation

def Fibonacci (n) :
    a, b = 0, 1
    print(a, end = ' ')
    print(b, end = ' ')

    for _ in range(n-2) :
        print(int(a+b), end = ' ')
        temp = a + b 
        a = b 
        b = temp
    
def main() :
    n = int(input("Generating first N Fibonacci numbers. Enter N : "))
    if n <= 1 : 
       for i in range(n) :
           print(i, end = ' ')
    else :
       Fibonacci(n)

if __name__ == "__main__" :
    main()

Output

Generating first N Fibonacci numbers. Enter N : 10
0 1 1 2 3 5 8 13 21 34 
Generating first N Fibonacci numbers. Enter N : 1
0
#include<iostream>

using namespace std;
typedef unsigned long long ULL;

void Fibonacci(ULL n) {
    
    ULL a = 0;
    ULL b = 1;
    cout << a << " " << b << " ";

    while (n > 2) {
        ULL next = a + b;
        cout << next << " ";
        a = b;  
        b = next;
        n--;
    }   
}

int main() {

    ULL n;
    cout << "Generating first N Fibonacci numbers. Enter N: ";
    cin >> n;
 
    if (n <= 2) {
       for (ULL i=0; i<n; i++) {
           cout << i << " "; 
       }
    } else { /* For n greater than 2 generate Fibonacci sequence */
       Fibonacci (n);
    }   
    return 0;
}

Output

Generating first N Fibonacci numbers. Enter N: 10
0 1 1 2 3 5 8 13 21 34 
Generating first N Fibonacci numbers. Enter N: 2
0 1
Generating first N Fibonacci numbers. Enter N: 5
0 1 1 2 3 
import java.util.Scanner;

class Fibonacci {

    public static void Generate_Fibonacci(long n) {

        long a = 0, b = 1;
        if ( n <= 0 ) { 
            return;
        } else if ( n == 1 ) { 
             System.out.print(a);
             return;
        } else if ( n >= 2 ) { 
             long next = 0;
             System.out.print(a + " " + b + " ");
             while ( n > 2 ) { 
                  next = a + b;
                  System.out.print(next + " ");
                  a = b;
                  b = next;
                  n--;
             }
        }
    }   

    public static void main (String[] args) {
        System.out.print("Generating the first N Fibonacci numbers.\nEnter N : ");
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        Generate_Fibonacci(n);
    }   
}

Output of Fibonacci sequence implemented in Java

Generating the first N Fibonacci numbers.
Enter N : 25
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368

Generating the first N Fibonacci numbers.
Enter N : 1
0



Copyright (c) 2019-2021, Algotree.org.
All rights reserved.