Skip to content

Added matrix exponentiation approach for finding fibonacci number. #1042

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
Jul 19, 2019

Conversation

mahbubcseju
Copy link
Contributor

  • Implemented matrix exponentiation for finding nth fibonnaci.
  • Complexity is about O(nlog(n)*8), here 8 is for matrix multiplication of size 2 by 2.

* Implemented the way of finding nth fibonacci.
* Complexity is about O(log(n)*8)
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Nice work. Thanks for your contribution.

In the filename and in the code, "Fibonacci" is sometimes spelled with two n. Please fix this so that it is properly spelled in all places.

- Removed some extra spaces    
- Added the complexity of bruteforce algorithm  
- Removed unused function called zerro()  
- Added some docktest based on request
@cclauss
Copy link
Member

cclauss commented Jul 19, 2019

from timeit import timeit
print(timeit("nth_fibonacci(1000)", "from __main__ import nth_fibonacci", number=10000))
print(timeit("nth_fibonacci_test(1000)", "from __main__ import nth_fibonacci_test", number=10000))

1.565457206
1.273862879

@mahbubcseju
Copy link
Contributor Author

from timeit import timeit
print(timeit("nth_fibonacci(1000)", "from __main__ import nth_fibonacci", number=10000))
print(timeit("nth_fibonacci_test(1000)", "from __main__ import nth_fibonacci_test", number=10000))

1.565457206
1.273862879

Yes . If the input is high like n=100000, you will get the difference.

@mahbubcseju
Copy link
Contributor Author

from timeit import timeit
print(timeit("nth_fibonacci(1000)", "from __main__ import nth_fibonacci", number=10000))
print(timeit("nth_fibonacci_test(1000)", "from __main__ import nth_fibonacci_test", number=10000))

1.565457206
1.273862879

from timeit import timeit
print(timeit("nth_fibonacci(1000000)", "from main import nth_fibonacci", number=5))
print(timeit("nth_fibonacci_test(1000000)", "from main import nth_fibonacci_test", number=5))
2.3342058970001744
57.256506615000035

@cclauss
Copy link
Member

cclauss commented Jul 19, 2019

Perhaps the functions should be renamed to clarify intent:

  • nth_fibonacci() --> nth_fibonacci_matrix()
  • nth_fibonacci__test() --> nth_fibonacci_bruteforce()

@mahbubcseju mahbubcseju force-pushed the master branch 3 times, most recently from 24df2cd to d30bbc1 Compare July 19, 2019 07:05
@mahbubcseju
Copy link
Contributor Author

  • nth_fibonacci_bruteforce

Changed

- Removed some extra spaces
- Added the complexity of bruteforce algorithm
- Removed unused function called zerro()
- Added some docktest based on request
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Awesome work here!!

@cclauss cclauss merged commit 60c608d into TheAlgorithms:master Jul 19, 2019
cclauss added a commit to cclauss/DroneFlight that referenced this pull request Jul 19, 2019
As a thank you for the nice work on TheAlgorithms/Python#1042...

These test will run on each update to the code.  You will need to log into https://travis-ci.org/mahbubcseju using your github credentials to turn on this free service.  This is a simple config (but it still catches one issue on Python 3).  To see a more complex config, see https://github.com/TheAlgorithms/Python/blob/master/.travis.yml
cclauss added a commit that referenced this pull request Jul 19, 2019
Deus-Absconditus added a commit to Deus-Absconditus/Python that referenced this pull request Nov 27, 2020
…heAlgorithms#1042)

* Added matrix exponentiation approach for finding fibonacci number.

* Implemented the way of finding nth fibonacci.
* Complexity is about O(log(n)*8)

* Updated the matrix exponentiation approach of finding nth fibonacci.

- Removed some extra spaces
- Added the complexity of bruteforce algorithm
- Removed unused function called zerro()
- Added some docktest based on request

* Updated the matrix exponentiation approach of finding nth fibonacci.

- Removed some extra spaces
- Added the complexity of bruteforce algorithm
- Removed unused function called zerro()
- Added some docktest based on request

* Tighten up main() and add comments on performance

Signed-off-by: Deus-Absconditus <[email protected]>
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…heAlgorithms#1042)

* Added matrix exponentiation approach for finding fibonacci number.

* Implemented the way of finding nth fibonacci.
* Complexity is about O(log(n)*8)

* Updated the matrix exponentiation approach of finding nth fibonacci.

- Removed some extra spaces    
- Added the complexity of bruteforce algorithm  
- Removed unused function called zerro()  
- Added some docktest based on request

* Updated the matrix exponentiation approach of finding nth fibonacci.

- Removed some extra spaces
- Added the complexity of bruteforce algorithm
- Removed unused function called zerro()
- Added some docktest based on request

* Tighten up main() and add comments on performance
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