Skip to content

Instantly share code, notes, and snippets.

@mactanxin
Forked from guxuerui/fib.js
Created June 22, 2020 08:07
Show Gist options
  • Save mactanxin/91c9487fcfaa1a9fee2e559e6842e870 to your computer and use it in GitHub Desktop.
Save mactanxin/91c9487fcfaa1a9fee2e559e6842e870 to your computer and use it in GitHub Desktop.
菲波那切数列
const fib = (num) => {
if (num <= 0) return 0;
if (num === 1 || num === 2) return 1;
if (num > 2) {
let prev = 1, next = 1;
for (let i = 3; i <= num; i++) {
let sum = prev + next;
prev = next;
next = sum;
}
return next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment