Fibonacci algorithm iterative
Jan 28, 2015 in Algorithms
from
http://www.csd.uwo.ca/courses/CS1027b/code/FibonacciDemo.java
/** Method to calculate the nth Fibonacci number iteratively. * @param n * @return nth Fibonacci number * precondition: n >= 1 */ public static long ifib(long n) { if (n < 2) { return 1; } else { long prev = 1, current = 1, next = 0; for (long i = 3; i <= n; i++) { next = prev + current; prev = current; current = next; } return next; } }