Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- public class Fib {
- public static void main (String[] args){
- System.out.println(fibnum(5));
- ArrayList <Integer> fibs = new ArrayList<Integer> ();
- //The integer passed to the constructor represents its initial capacity,
- //i.e., the number of elements it can hold before it needs to resize its internal array
- //(and has nothing to do with the initial number of elements in the list).
- ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(60, 0));
- for (int i = 0; i < 99; i++){
- if (i < 2) fibs.add(1);
- else fibs.add(fibs.get(i-1) + fibs.get(i-2));
- }
- System.out.println(fibs.toString());
- // http://introcs.cs.princeton.edu/java/96optimization/Fibonacci.java.html
- int N = Integer.parseInt(args[0]);
- if (N < 1 || N > 92) {
- throw new RuntimeException("N must be between 1 and 92");
- }
- long[] fib = new long[N+1];
- // base cases
- fib[0] = 0;
- fib[1] = 1;
- // bottom-up dynamic programming
- for (int n = 2; n <= N; n++)
- fib[n] = fib[n-1] + fib[n-2];
- // print results
- System.out.println(fib[N]);
- }
- public static int fibnum (int n){
- if (n<2) return 1;
- else return fibnum (n-1) + fibnum (n-2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement