Skip to content

Instantly share code, notes, and snippets.

@aniruddha84
Created January 17, 2018 21:06
Show Gist options
  • Save aniruddha84/947a46642054fe727d86e8750fb8cf0d to your computer and use it in GitHub Desktop.
Save aniruddha84/947a46642054fe727d86e8750fb8cf0d to your computer and use it in GitHub Desktop.
Nth Fibonacci number
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