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

Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions fuzzy_logic/fuzzy_operations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""README, Author - Jigyasa Gandhi(mailto:jigsgandhi97@gmail.com)
"""
README, Author - Jigyasa Gandhi(mailto:jigsgandhi97@gmail.com)
Requirements:
- scikit-fuzzy
- numpy
Expand All @@ -7,7 +8,11 @@
- 3.5
"""
import numpy as np
import skfuzzy as fuzz

try:
import skfuzzy as fuzz
except ImportError:
fuzz = None

if __name__ == "__main__":
# Create universe of discourse in Python using linspace ()
Expand Down
22 changes: 12 additions & 10 deletions project_euler/problem_014/sol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,27 @@
"""
from __future__ import annotations

COLLATZ_SEQUENCE_LENGTHS = {1: 1}


def collatz_sequence_length(n: int) -> int:
"""Returns the Collatz sequence length for n."""
sequence_length = 1
while n != 1:
if n % 2 == 0:
n //= 2
else:
n = 3 * n + 1
sequence_length += 1
if n in COLLATZ_SEQUENCE_LENGTHS:
return COLLATZ_SEQUENCE_LENGTHS[n]
if n % 2 == 0:
next_n = n // 2
else:
next_n = 3 * n + 1
sequence_length = collatz_sequence_length(next_n) + 1
COLLATZ_SEQUENCE_LENGTHS[n] = sequence_length
Comment on lines +41 to +42
Copy link

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
Member Author

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

return sequence_length


def solution(n: int = 1000000) -> int:
"""Returns the number under n that generates the longest Collatz sequence.

# The code below has been commented due to slow execution affecting Travis.
# >>> solution(1000000)
# 837799
>>> solution(1000000)
837799
>>> solution(200)
171
>>> solution(5000)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pandas
pillow
qiskit
requests
scikit-fuzzy
# scikit-fuzzy # Causing broken builds
sklearn
statsmodels
sympy
Expand Down