Skip to content

tech(recursion/nth-fibonacci): fibonacci #2

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

Merged
merged 4 commits into from
Jun 22, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions recursion-nth-fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# O(n) time | O(1) space
def getNthFib(n):
defaultArray = [0,1]
count = 3
while count <= n:
sumNo = defaultArray[0] + defaultArray[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change sumNo variable to sum

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

defaultArray[0] = defaultArray[1]
defaultArray[1] = sumNo
count = count + 1
if n == 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this if-block before count init since there is no need for count assignment and further while check. correct me if I am wrong

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOne

return defaultArray[0]
else:
return defaultArray[1]