File tree 3 files changed +20
-13
lines changed
project_euler/problem_014
3 files changed +20
-13
lines changed Original file line number Diff line number Diff line change 1
- """README, Author - Jigyasa Gandhi(mailto:[email protected] )
1
+ """
2
+ README, Author - Jigyasa Gandhi(mailto:[email protected] )
2
3
Requirements:
3
4
- scikit-fuzzy
4
5
- numpy
7
8
- 3.5
8
9
"""
9
10
import numpy as np
10
- import skfuzzy as fuzz
11
+
12
+ try :
13
+ import skfuzzy as fuzz
14
+ except ImportError :
15
+ fuzz = None
11
16
12
17
if __name__ == "__main__" :
13
18
# Create universe of discourse in Python using linspace ()
Original file line number Diff line number Diff line change 27
27
"""
28
28
from __future__ import annotations
29
29
30
+ COLLATZ_SEQUENCE_LENGTHS = {1 : 1 }
31
+
30
32
31
33
def collatz_sequence_length (n : int ) -> int :
32
34
"""Returns the Collatz sequence length for n."""
33
- sequence_length = 1
34
- while n != 1 :
35
- if n % 2 == 0 :
36
- n //= 2
37
- else :
38
- n = 3 * n + 1
39
- sequence_length += 1
35
+ if n in COLLATZ_SEQUENCE_LENGTHS :
36
+ return COLLATZ_SEQUENCE_LENGTHS [n ]
37
+ if n % 2 == 0 :
38
+ next_n = n // 2
39
+ else :
40
+ next_n = 3 * n + 1
41
+ sequence_length = collatz_sequence_length (next_n ) + 1
42
+ COLLATZ_SEQUENCE_LENGTHS [n ] = sequence_length
40
43
return sequence_length
41
44
42
45
43
46
def solution (n : int = 1000000 ) -> int :
44
47
"""Returns the number under n that generates the longest Collatz sequence.
45
48
46
- # The code below has been commented due to slow execution affecting Travis.
47
- # >>> solution(1000000)
48
- # 837799
49
+ >>> solution(1000000)
50
+ 837799
49
51
>>> solution(200)
50
52
171
51
53
>>> solution(5000)
Original file line number Diff line number Diff line change 9
9
pillow
10
10
qiskit
11
11
requests
12
- scikit-fuzzy
12
+ # scikit-fuzzy # Causing broken builds
13
13
sklearn
14
14
statsmodels
15
15
sympy
You can’t perform that action at this time.
0 commit comments