mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Improve Project Euler problem 014 solution 2 (#5744)
* Improve solution * Uncomment code that has been commented due to slow execution affecting Travis * Fix * scikit-fuzzy is causing broken builds * fuzz = None * Update fuzzy_operations.py Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
7a605766fe
commit
729aaf6427
@ -1,4 +1,5 @@
|
|||||||
"""README, Author - Jigyasa Gandhi(mailto:jigsgandhi97@gmail.com)
|
"""
|
||||||
|
README, Author - Jigyasa Gandhi(mailto:jigsgandhi97@gmail.com)
|
||||||
Requirements:
|
Requirements:
|
||||||
- scikit-fuzzy
|
- scikit-fuzzy
|
||||||
- numpy
|
- numpy
|
||||||
@ -7,7 +8,11 @@ Python:
|
|||||||
- 3.5
|
- 3.5
|
||||||
"""
|
"""
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import skfuzzy as fuzz
|
|
||||||
|
try:
|
||||||
|
import skfuzzy as fuzz
|
||||||
|
except ImportError:
|
||||||
|
fuzz = None
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Create universe of discourse in Python using linspace ()
|
# Create universe of discourse in Python using linspace ()
|
||||||
|
@ -27,25 +27,27 @@ Which starting number, under one million, produces the longest chain?
|
|||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
COLLATZ_SEQUENCE_LENGTHS = {1: 1}
|
||||||
|
|
||||||
|
|
||||||
def collatz_sequence_length(n: int) -> int:
|
def collatz_sequence_length(n: int) -> int:
|
||||||
"""Returns the Collatz sequence length for n."""
|
"""Returns the Collatz sequence length for n."""
|
||||||
sequence_length = 1
|
if n in COLLATZ_SEQUENCE_LENGTHS:
|
||||||
while n != 1:
|
return COLLATZ_SEQUENCE_LENGTHS[n]
|
||||||
if n % 2 == 0:
|
if n % 2 == 0:
|
||||||
n //= 2
|
next_n = n // 2
|
||||||
else:
|
else:
|
||||||
n = 3 * n + 1
|
next_n = 3 * n + 1
|
||||||
sequence_length += 1
|
sequence_length = collatz_sequence_length(next_n) + 1
|
||||||
|
COLLATZ_SEQUENCE_LENGTHS[n] = sequence_length
|
||||||
return sequence_length
|
return sequence_length
|
||||||
|
|
||||||
|
|
||||||
def solution(n: int = 1000000) -> int:
|
def solution(n: int = 1000000) -> int:
|
||||||
"""Returns the number under n that generates the longest Collatz sequence.
|
"""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)
|
||||||
# >>> solution(1000000)
|
837799
|
||||||
# 837799
|
|
||||||
>>> solution(200)
|
>>> solution(200)
|
||||||
171
|
171
|
||||||
>>> solution(5000)
|
>>> solution(5000)
|
||||||
|
@ -9,7 +9,7 @@ pandas
|
|||||||
pillow
|
pillow
|
||||||
qiskit
|
qiskit
|
||||||
requests
|
requests
|
||||||
scikit-fuzzy
|
# scikit-fuzzy # Causing broken builds
|
||||||
sklearn
|
sklearn
|
||||||
statsmodels
|
statsmodels
|
||||||
sympy
|
sympy
|
||||||
|
Loading…
Reference in New Issue
Block a user