Skip to content

Commit 3d41723

Browse files
MarkMorettocclauss
andauthored
project_euler/problem_47/sol1.py (TheAlgorithms#2150)
* Create __init__.py * Initial commit Not sure if this should be formatted differently. I'm open to ideas! * Completing testing/updates Ran code through `black`, `flake8`, and `doctest`. Added some type hints. `doctest` is finicky on sets, so I had to sort and reformat as set to pass those tests. * Update project_euler/problem_47/sol1.py Nice. Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py Looks good Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py Okay, this should work. Thank you for the reminder on map(), filter(), reduce(). Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py My IDE needs a spellchecker. Or, lighter comment font. Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py This means that `results = run(N)` should be updated to `results = run(n)`, correct? Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py Looks good! Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_47/sol1.py Works for me! I spent way too much time getting this to pass doctest, so any improvement is welcome. Co-authored-by: Christian Clauss <cclauss@me.com> * Update sol1.py Added some suggested changes from the pull request: * Updated tests outputs in `unique_prime_factors` function. * Changed `@lru_cache(maxsize=5)` to `@lru_cache(maxsize=None)` * Removed duplicate `return` line in `equality` function * Changed `i` to `base` in run function. * Added some commentary to `run()` function. * Replaced `group = list(map(lambda x: base + x, [i for i in range(n)]))` with `group = [base + i for i in range(n)]` * Update sol1.py * Trailing whitespace * Update sol1.py * Update __init__.py * Update sol1.py * Update __init__.py Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 9eb3138 commit 3d41723

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

project_euler/problem_47/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

project_euler/problem_47/sol1.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
Combinatoric selections
3+
4+
Problem 47
5+
6+
The first two consecutive numbers to have two distinct prime factors are:
7+
8+
14 = 2 × 7
9+
15 = 3 × 5
10+
11+
The first three consecutive numbers to have three distinct prime factors are:
12+
13+
644 = 2² × 7 × 23
14+
645 = 3 × 5 × 43
15+
646 = 2 × 17 × 19.
16+
17+
Find the first four consecutive integers to have four distinct prime factors each.
18+
What is the first of these numbers?
19+
"""
20+
21+
from functools import lru_cache
22+
23+
24+
def unique_prime_factors(n: int) -> set:
25+
"""
26+
Find unique prime factors of an integer.
27+
Tests include sorting because only the set really matters,
28+
not the order in which it is produced.
29+
>>> sorted(set(unique_prime_factors(14)))
30+
[2, 7]
31+
>>> set(sorted(unique_prime_factors(644)))
32+
[2, 7, 23]
33+
>>> set(sorted(unique_prime_factors(646)))
34+
[2, 17, 19]
35+
"""
36+
i = 2
37+
factors = set()
38+
while i * i <= n:
39+
if n % i:
40+
i += 1
41+
else:
42+
n //= i
43+
factors.add(i)
44+
if n > 1:
45+
factors.add(n)
46+
return factors
47+
48+
49+
@lru_cache
50+
def upf_len(num: int) -> int:
51+
"""
52+
Memoize upf() length results for a given value.
53+
>>> upf_len(14)
54+
2
55+
"""
56+
return len(unique_prime_factors(num))
57+
58+
59+
def equality(iterable: list) -> bool:
60+
"""
61+
Check equality of ALL elements in an interable.
62+
>>> equality([1, 2, 3, 4])
63+
False
64+
>>> equality([2, 2, 2, 2])
65+
True
66+
>>> equality([1, 2, 3, 2, 1])
67+
True
68+
"""
69+
return len(set(iterable)) in (0, 1)
70+
71+
72+
def run(n: int) -> list:
73+
"""
74+
Runs core process to find problem solution.
75+
>>> run(3)
76+
[644, 645, 646]
77+
"""
78+
79+
# Incrementor variable for our group list comprehension.
80+
# This serves as the first number in each list of values
81+
# to test.
82+
base = 2
83+
84+
while True:
85+
# Increment each value of a generated range
86+
group = [base + i for i in range(n)]
87+
88+
# Run elements through out unique_prime_factors function
89+
# Append our target number to the end.
90+
checker = [upf_len(x) for x in group]
91+
checker.append(n)
92+
93+
# If all numbers in the list are equal, return the group variable.
94+
if equality(checker):
95+
return group
96+
97+
# Increment our base variable by 1
98+
base += 1
99+
100+
101+
def solution(n: int = 4) -> int:
102+
"""Return the first value of the first four consecutive integers to have four
103+
distinct prime factors each.
104+
>>> solution()
105+
134043
106+
"""
107+
results = run(n)
108+
return results[0] if len(results) else None
109+
110+
111+
if __name__ == "__main__":
112+
print(solution())

0 commit comments

Comments
 (0)