[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/oribach/Java/master/DynamicProgramming/Fibonacci.java [Back]  [Original]

package DynamicProgramming;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/** @author Varun Upadhyay (https://github.com/varunu28) */
public class Fibonacci {

  private static Map map = new HashMap();

  public static void main(String[] args) {

    // Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    System.out.println(fibMemo(n));
    System.out.println(fibBotUp(n));
    System.out.println(fibOptimized(n));
    sc.close();
  }

  /**
   * This method finds the nth fibonacci number using memoization technique
   *
   * @param n The input n for which we have to determine the fibonacci number Outputs the nth
   *     fibonacci number
   */
  public static int fibMemo(int n) {
    if (map.containsKey(n)) {
      return map.get(n);
    }

    int f;

    if (n 

Web Proxy Viewer  |  New URL  |  Original Page