Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

求斐波拉契数列的迭代方式,是否正确? #29

Closed
PachVerb opened this issue Nov 16, 2021 · 1 comment
Closed

求斐波拉契数列的迭代方式,是否正确? #29

PachVerb opened this issue Nov 16, 2021 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@PachVerb
Copy link
Contributor

问题定位于: stack/utils.ts

function fibonacciIterative(n: number) {
  if ((n = 0)) return 0;
  if ((n = 1)) return 1;

  let fibNMinus2 = 0;
  let fibNMinus1 = 1;
  let fibN = n;
  for (let i = 2; i < n; i++) {
    fibN = fibNMinus1 + fibNMinus2;
    // why fibNminus2 is not update?
    fibNMinus1 = fibN;
  }

  return fibN;
}
@PachVerb
Copy link
Contributor Author

问题定位于: stack/utils.ts

function fibonacciIterative(n: number) {
  if ((n = 0)) return 0;
  if ((n = 1)) return 1;

  let fibNMinus2 = 0;
  let fibNMinus1 = 1;
  let fibN = n;
  for (let i = 2; i < n; i++) {
    fibN = fibNMinus1 + fibNMinus2;
    // why fibNminus2 is not update?
    fibNMinus1 = fibN;
  }

  return fibN;
}

循环迭代时,未更新变量fibNMinus2,会导致迭代出来的数列,非非波拉契数列 👍

@PachVerb PachVerb self-assigned this Nov 16, 2021
@PachVerb PachVerb added the bug Something isn't working label Nov 16, 2021
@PachVerb PachVerb pinned this issue Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant