Skip to content

Instantly share code, notes, and snippets.

@kntmr
Created September 4, 2019 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kntmr/628e35811821855921fd3e161ca77f2e to your computer and use it in GitHub Desktop.
Save kntmr/628e35811821855921fd3e161ca77f2e to your computer and use it in GitHub Desktop.
Fibonacci sequence
package com.example.demo;
public class Fib {
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 2) + fib(n - 1);
}
int fib(int n, int[] m) {
if (n == 0) return 0;
if (n == 1) return 1;
if (m[n] == 0) {
m[n] = fib(n - 2, m) + fib(n - 1, m);
}
return m[n];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment