The Wayback Machine - https://web.archive.org/web/20201006213136/https://github.com/TheAlgorithms/Python/pull/2619
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 fibonacci_sequence_recursion_with_memoization.py #2619

Open
wants to merge 17 commits into
base: master
from

Conversation

@SamueldaCostaAraujoNunes
Copy link

@SamueldaCostaAraujoNunes SamueldaCostaAraujoNunes commented Oct 1, 2020

My algorithm relates the application of the Fibonacci sequence (using Recursion) to the Memoization technique. Thus, it is possible to reach the complexity O (1), to calculate the Fibonacci value of very high numbers and in an incredibly shorter time interval than using only the common recursion.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.
@SamueldaCostaAraujoNunes SamueldaCostaAraujoNunes changed the title Fibonacci using Memoization and Recursion Create fibonacci_sequence_recursion_with_memoization.py Oct 1, 2020
@TravisBuddy
Copy link

@TravisBuddy TravisBuddy commented Oct 1, 2020

Travis tests have failed

Hey @SamueldaCostaAraujoNunes,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 6d896530-0414-11eb-b614-dd2b96524a99
@TravisBuddy
Copy link

@TravisBuddy TravisBuddy commented Oct 1, 2020

Travis tests have failed

Hey @SamueldaCostaAraujoNunes,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 6972fd20-0415-11eb-b614-dd2b96524a99
@TravisBuddy
Copy link

@TravisBuddy TravisBuddy commented Oct 1, 2020

Travis tests have failed

Hey @SamueldaCostaAraujoNunes,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 8bec1310-0419-11eb-b614-dd2b96524a99
@TravisBuddy
Copy link

@TravisBuddy TravisBuddy commented Oct 1, 2020

Travis tests have failed

Hey @SamueldaCostaAraujoNunes,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 3ce5bfd0-041b-11eb-b614-dd2b96524a99
Return number of index n in sequence fibonacci
>>> [fibonacci(i) for i in range(12)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
"""
Comment on lines 11 to 14

This comment has been minimized.

@dhruvmanila

dhruvmanila Oct 2, 2020
Member

Please have multiple doctest.


if aux in cache:
return cache[aux]
Comment on lines 16 to 18

This comment has been minimized.

@dhruvmanila

dhruvmanila Oct 2, 2020
Member

Please check whether the input is of type int, if not then raise TypeError and for negative numbers raise ValueError.

if aux in cache:
return cache[aux]

if (n == 0) or (n == 1):

This comment has been minimized.

@dhruvmanila

dhruvmanila Oct 2, 2020
Member
Suggested change
if (n == 0) or (n == 1):
if n in {0, 1}:

set are hashed, so fast lookup.

$ python -m timeit -s "n = 1" "(n == 0) or (n == 1)"
10000000 loops, best of 5: 32.5 nsec per loop

$ python -m timeit -s "n = 1" "n in {0, 1}"
10000000 loops, best of 5: 21 nsec per loop

$ python -m timeit -s "n = 1" "n in (0, 1)"
10000000 loops, best of 5: 27.4 nsec per loop



def main() -> None:
number = int(input("Enter an integer greater than or equal to 0: "))

This comment has been minimized.

@dhruvmanila

dhruvmanila Oct 2, 2020
Member
Suggested change
number = int(input("Enter an integer greater than or equal to 0: "))
try:
number = int(input("Enter an integer greater than or equal to 0: "))
except ValueError:
print("Error: Input value should be an integer greater than or equal to 0")

This comment has been minimized.

@NumberPiOso

NumberPiOso Oct 2, 2020
Contributor

This try does not assert that the value is greater than or equal to 0, so the message of error is misleading.

Co-authored-by: Dhruv <[email protected]>
@NumberPiOso
Copy link
Contributor

@NumberPiOso NumberPiOso commented Oct 2, 2020

A simpler approach is to use the decorator
from functools import lru_cache

This uses the memoization technique withouth the overhead of having to implement the dictionary and looking for values.

There is a example of this for Fibonacci numbers in the Python documentation.
https://docs.python.org/3/library/functools.html

@lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)
@TravisBuddy
Copy link

@TravisBuddy TravisBuddy commented Oct 2, 2020

Travis tests have failed

Hey @SamueldaCostaAraujoNunes,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: d5a554f0-04c6-11eb-a4bc-1b94c014fd22
@TravisBuddy
Copy link

@TravisBuddy TravisBuddy commented Oct 2, 2020

Travis tests have failed

Hey @SamueldaCostaAraujoNunes,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 5ce50cd0-04c7-11eb-a4bc-1b94c014fd22
@TravisBuddy
Copy link

@TravisBuddy TravisBuddy commented Oct 2, 2020

Travis tests have failed

Hey @SamueldaCostaAraujoNunes,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 01cb2440-04c9-11eb-a4bc-1b94c014fd22
@SamueldaCostaAraujoNunes
Copy link
Author

@SamueldaCostaAraujoNunes SamueldaCostaAraujoNunes commented Oct 2, 2020

A simpler approach is to use the decorator
from functools import lru_cache

This uses the memoization technique withouth the overhead of having to implement the dictionary and looking for values.

There is a example of this for Fibonacci numbers in the Python documentation.
https://docs.python.org/3/library/functools.html

@lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

I really agree that it would be much simpler to implement, but my intention with this code was to be more didactic, applying memoization directly within the Fibonacci sequence! I liked to know that there is a native solution within Python itself, but I chose to keep the logic of my algorithm in this way, thank you!

@poyea
Copy link
Member

@poyea poyea commented Oct 5, 2020

Thanks. Did you take a look at the existing implementation in dynamic programming? What's the major difference between that two versions and yours?

@SamueldaCostaAraujoNunes
Copy link
Author

@SamueldaCostaAraujoNunes SamueldaCostaAraujoNunes commented Oct 5, 2020

Thanks. Did you take a look at the existing implementation in dynamic programming? What's the major difference between that two versions and yours?

My program is in the application of memoization in the fibonacci sequence using a single recursive function. The fast_fibonacci example applies recursion, but does not apply any algorithm focused on saving data, as is done in the memoizationtechnique. In the fibonacci example, an array is used that will be generated at the beginning of the program execution, with the size defined by the user and only after that, that the user can request the desired fibonacci value! My program uses a dictionary to store the corresponding values ​​of the fibonacci sequence, only when necessary! So, my program allows you to find the desired value in O (1), right on the first run, and it is possible to save this same dictionary for future runs! The limitation of this program is due to the amount of memory available in the system!

@poyea
Copy link
Member

@poyea poyea commented Oct 6, 2020

Thanks. Did you take a look at the existing implementation in dynamic programming? What's the major difference between that two versions and yours?

My program is in the application of memoization in the fibonacci sequence using a single recursive function. The fast_fibonacci example applies recursion, but does not apply any algorithm focused on saving data, as is done in the memoizationtechnique. In the fibonacci example, an array is used that will be generated at the beginning of the program execution, with the size defined by the user and only after that, that the user can request the desired fibonacci value! My program uses a dictionary to store the corresponding values ​​of the fibonacci sequence, only when necessary! So, my program allows you to find the desired value in O (1), right on the first run, and it is possible to save this same dictionary for future runs! The limitation of this program is due to the amount of memory available in the system!

Yes, but is it still, classified as dynamic programming?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.