-
-
Save mactanxin/91c9487fcfaa1a9fee2e559e6842e870 to your computer and use it in GitHub Desktop.
菲波那切数列
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
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