-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
project_euler/problem_47/sol1.py #2150
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
Changes from 3 commits
f225be4
388bec4
71b432e
61cbe18
ce4d670
2449898
0947926
8a4cdf9
3d10e34
e96cb25
0bb6352
c3d3887
97bcaaa
1c2ef0b
3cdc62c
2df1f5a
def54a9
9603f48
7443e13
b148bec
3b7e0b0
fe30329
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,111 @@ | ||||
""" | ||||
Combinatoric selections | ||||
|
||||
Problem 47 | ||||
|
||||
The first two consecutive numbers to have two distinct prime factors are: | ||||
|
||||
14 = 2 × 7 | ||||
15 = 3 × 5 | ||||
|
||||
The first three consecutive numbers to have three distinct prime factors are: | ||||
|
||||
644 = 2² × 7 × 23 | ||||
645 = 3 × 5 × 43 | ||||
646 = 2 × 17 × 19. | ||||
|
||||
Find the first four consecutive integers to have four distinct prime factors each. | ||||
What is the first of these numbers? | ||||
""" | ||||
|
||||
from functools import lru_cache | ||||
|
||||
|
||||
def unique_prime_factors(n: int) -> set: | ||||
""" | ||||
Function to find unique prime factors of an integer. | ||||
Tests include sorting because only the set really matters, | ||||
not the order in which it is produced. | ||||
>>> set(sorted(unique_prime_factors(14))) | ||||
{2, 7} | ||||
>>> set(sorted(unique_prime_factors(644))) | ||||
{2, 23, 7} | ||||
>>> set(sorted(unique_prime_factors(646))) | ||||
{17, 2, 19} | ||||
""" | ||||
i = 2 | ||||
factors = set() | ||||
while i * i <= n: | ||||
if n % i: | ||||
i += 1 | ||||
else: | ||||
n //= i | ||||
factors.add(i) | ||||
if n > 1: | ||||
factors.add(n) | ||||
return factors | ||||
|
||||
|
||||
@lru_cache(maxsize=5) | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
def upf_len(num: int) -> int: | ||||
""" | ||||
Helper function to memoize upf() length results for a given value. | ||||
>>> upf_len(14) | ||||
2 | ||||
""" | ||||
return len(unique_prime_factors(num)) | ||||
|
||||
|
||||
def equality(iterable: list) -> bool: | ||||
""" | ||||
Check equality of ALL elements in an interable. | ||||
>>> equality([1,2,3,4]) | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
False | ||||
>>> equality([2,2,2,2]) | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
True | ||||
""" | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
if iterable[:1]: | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
return iterable[1:] == iterable[:-1] | ||||
|
||||
|
||||
def run(n: int) -> list: | ||||
""" | ||||
Function that runs core process to find problem solution. | ||||
>>> run(3) | ||||
[644, 645, 646] | ||||
""" | ||||
|
||||
i = 2 | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The If I can change the variable to
|
||||
|
||||
success = 0 | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
while success < 1: | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
# Increment each value of a generated range | ||||
group = list(map(lambda x, y=i: y + x, [i for i in range(n)])) | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
# Run elements through out unique_prime_factors function | ||||
# Append our target number to the end. | ||||
checker = list(map(upf_len, group)) | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
checker.append(n) | ||||
|
||||
# If all numbers in the list are euqal, increment our success variable | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
# to exit the while loop and return the current group of numbers. | ||||
if equality(checker): | ||||
success += 1 | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
return group | ||||
i += 1 | ||||
|
||||
|
||||
def solution(N: int = 4) -> int: | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
"""Returns the first value of the first four consecutive integers to have four | ||||
distinct prime factors each. | ||||
>>> solution() | ||||
134043 | ||||
""" | ||||
results = run(N) | ||||
if len(results) > 0: | ||||
MarkMoretto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
return results[0] | ||||
|
||||
|
||||
if __name__ == "__main__": | ||||
print(solution()) |
Uh oh!
There was an error while loading. Please reload this page.