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

Create fastDoublingNthFibonacci.py #160

Merged
merged 1 commit into from
Oct 19, 2019
Merged

Create fastDoublingNthFibonacci.py #160

merged 1 commit into from
Oct 19, 2019

Conversation

2knal
Copy link
Contributor

@2knal 2knal commented Oct 15, 2019

I am adding a function (NthFibonacci), with which we can find the nth Fibonacci number in O(logn) time. (#126)

Copy link
Member

@dnery dnery left a comment

Choose a reason for hiding this comment

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

Hey @2knal, thanks for the contribution! 😄

Happy hacking!


Fibonacci series: 0 1 1 2 3 ...
NthFibonacci(n, mod) -> int
mod: We find modulo of number (usually with 1e9+7), as it is too big.
Copy link
Member

Choose a reason for hiding this comment

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

Very interesting!

I didn't know about that programming pattern...

Thanks for sharing! 😄

c = (a%mod * ((b*2)%mod - a%mod))%mod
d = ((a%mod * a%mod)%mod + (b%mod * b%mod)%mod)%mod

if not n&1:
Copy link
Member

Choose a reason for hiding this comment

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

( ͡° ͜ʖ ͡°)

@dnery dnery self-assigned this Oct 19, 2019
@dnery dnery merged commit a60ff91 into pirataspinhal:master Oct 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants