Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class fibonacci {
- public static int fib (int n) {
- int x = 0;
- if (n == 0)
- x = 0;
- else if (n == 1)
- x = 1;
- else {
- x = fib (n-1) + fib (n-2);
- }
- return x;
- }
- public static void main (String args[]) {
- int num = 0;
- int result = 0;
- long start = 0L;
- double duration = 0.0;
- Scanner stdin = new Scanner(System.in);
- System.out.println("\n--- Java Recursive Ficonacci test ---\n");
- System.out.print("\nEnter initial value: ");
- num = stdin.nextInt();
- // high resolution timer (nanoseconds)
- start = System.nanoTime();
- result = fib (num);
- // converting nanoseconds to seconds: nanosecond = 1 billion seconds
- duration = ((double) System.nanoTime() - start) / 1000000000.0;
- System.out.println("\n\tFibonacci("+ num +") = "+ result + "\n");
- System.out.printf("\n\t...finished in %4.6f seconds. \n\n", duration);
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement