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

Improve Project Euler problem 014 solution 2 #5744

Conversation

MaximSmolskiy
Copy link
Contributor

@MaximSmolskiy MaximSmolskiy commented Nov 1, 2021

Describe your change:

Improve Project Euler problem 014 solution 2 - the top 1 slowest solution on Travis CI logs (under slowest 10 durations: 14.20s call scripts/validate_solutions.py::test_project_euler[problem_014/sol2.py]):

  • Improve solution (locally 10+ times - from 15+ seconds to ~1.5 seconds)

  • Uncomment code that has been commented due to slow execution affecting Travis (now it should be quite fast execution and not affect Travis)

  • 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}.

@algorithms-keeper algorithms-keeper bot added awaiting reviews enhancement labels Nov 1, 2021
@algorithms-keeper algorithms-keeper bot added tests are failing and removed tests are failing labels Nov 1, 2021
IdoErel
IdoErel approved these changes Nov 1, 2021
Copy link

@IdoErel IdoErel left a comment

Nice work overall. You replaced the loop with recursion, possibly making the code faster. Also, having the sequence lengths as a global dictionary would save a lot of time computing.

Maybe note such changes in the commit message next time (E.g. loop to recursion).

Sincerely,
IdoErel

sequence_length = collatz_sequence_length(next_n) + 1
COLLATZ_SEQUENCE_LENGTHS[n] = sequence_length
Copy link

@IdoErel IdoErel Nov 1, 2021

Choose a reason for hiding this comment

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

Unnecessary variable assignment.
This can be shortened to one line.

Copy link
Contributor Author

@MaximSmolskiy MaximSmolskiy Nov 1, 2021

Choose a reason for hiding this comment

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

But then we should return COLLATZ_SEQUENCE_LENGTHS[n] instead of sequence_length. I don't know Python perfectly - whether there will be an extra search in the dictionary or not?

Copy link

@IdoErel IdoErel Nov 3, 2021

Choose a reason for hiding this comment

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

No, you're right. I missed line 43.
Disregard my previous comment.

I originally meant, that assigning the result to sequence_length - and immediately after assigning it again to COLLATZ_SEQUENCE_LENGTHS, without any further usage is unnecessary. (of sequence_length).

However, you're returning sequence_length as the result. Saving another lookup - in short, keep it this way. I was wrong.

Sorry for my ignorance.

Sincerely,
IdoErel

@algorithms-keeper algorithms-keeper bot added the tests are failing label Nov 4, 2021
cclauss
cclauss approved these changes Nov 4, 2021
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews label Nov 4, 2021
@cclauss cclauss merged commit 729aaf6 into TheAlgorithms:master Nov 4, 2021
4 checks passed
@MaximSmolskiy MaximSmolskiy deleted the improve-project-euler-problem-014-solution-2 branch Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement tests are failing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants