Friday, 6 June 2014

Java 8 : Fibonacci Series

Three techniques to implement Fibonacci Series in Java:

  1. Fibonacci Series With Iteration Approach
  2. Fibonacci Series With Recursion Approach
  3. Fibonacci Series With Java 8 Collectors Approach

Fibonacci Series With Iteration Approach Example:

import java.util.Scanner;

public class FibonacciWithIteration
{

public static void main(String[] args)
  {

System.out.println("Enter A Number Upto Which Fibonacci Series You Want = ");

int num = new Scanner(System.in).nextInt();      

       System.out.println("Fibonacci Series With Iteration Approach Upto " + num +" numbers = ");
        for(int i=1; i <= num; i++)
       {
            System.out.print(fibonacciWithIteration(i) +" ");
        }
}
   public static int fibonacciWithIteration(int num)
    {
        if(num == 1 || num == 2){
            return 1;
        }
        int fibo1=1, fibo2=1, fibonacci=1;
        for(int i= 3; i<= num; i++)
        {
            fibonacci = fibo1 + fibo2; 
            fibo1 = fibo2;
            fibo2 = fibonacci;          
        }
        return fibonacci;      
    }   
}

Output:

Enter A Number Upto Which Fibonacci Series You Want = 
4
Fibonacci Series With Iteration Approach Upto 4 numbers = 
1 1 2 3 



Fibonacci Series With Recursion Approach Example:

import java.util.Scanner;
public class FibonacciWithRecursion
{
public static void main(String[] args)
    {
System.out.println("Enter A Number Upto Which Fibonacci Series You Want = ");
int num = new Scanner(System.in).nextInt();      
        System.out.println("Fibonacci Series With Recursion Approach Upto " + num +" numbers = ");
        for(int i=1; i <= num; i++)
        {
            System.out.print(fibonacciWithRecursion(i) +" ");
        }
}
    public static int fibonacciWithRecursion(int num)
    {
    if(num == 1 || num == 2)
        {
            return 1;
        }
      
        return fibonacciWithRecursion(num-1) + fibonacciWithRecursion(num -2);  
    }   
}


Output:

Enter A Number Upto Which Fibonacci Series You Want = 
4
Fibonacci Series With Iteration Approach Upto 4 numbers = 
1 1 2 3 

Fibonacci Series With Java 8 Collectors Approach Example:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class FibonacciWithCollectors
{
    private final Map<Integer,Long> cache;
    public FibonacciWithCollectors()
   {
        cache = new HashMap<>();
        cache.put(0, 0L);
        cache.put(1, 1L);
    }
    public long fibonacci(int x)
    {
        return cache.computeIfAbsent(x, n -> fibonacci(n-1) + fibonacci(n-2));
    }
    public static void main(String[] args)
    {
    System.out.println("Enter A Number Upto Which Fibonacci Series You Want = ");
int num = new Scanner(System.in).nextInt();      
        System.out.println("Fibonacci Series With Collectors Approach Upto " + num +" numbers = ");
        FibonacciWithCollectors f = new FibonacciWithCollectors();
        for(int i=1; i <= num; i++)
        {
            System.out.print(f.fibonacci(i) +" ");
        }
}
}

Output:

Enter A Number Upto Which Fibonacci Series You Want = 
4
Fibonacci Series With Collectors Approach Upto 4 numbers = 
1 1 2 3