Created
January 17, 2018 21:06
-
-
Save aniruddha84/947a46642054fe727d86e8750fb8cf0d to your computer and use it in GitHub Desktop.
Nth Fibonacci number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int nfibonacci(int n) { | |
if (n == 1 || n == 2) return 1; | |
if (n < 1) throw new RuntimeException("Invalid parameter. n must be greater than 0"); | |
int i = 1; | |
int j = 1; | |
int result = 0; | |
for(int k = 3; k <= n; k++) { | |
result = i + j; | |
j = i; | |
i = result; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment