-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f225be4
Create __init__.py
MarkMoretto 388bec4
Initial commit
MarkMoretto 71b432e
Completing testing/updates
MarkMoretto 61cbe18
Update project_euler/problem_47/sol1.py
MarkMoretto ce4d670
Update project_euler/problem_47/sol1.py
MarkMoretto 2449898
Update project_euler/problem_47/sol1.py
MarkMoretto 0947926
Update project_euler/problem_47/sol1.py
MarkMoretto 8a4cdf9
Update project_euler/problem_47/sol1.py
MarkMoretto 3d10e34
Update project_euler/problem_47/sol1.py
MarkMoretto e96cb25
Update project_euler/problem_47/sol1.py
MarkMoretto 0bb6352
Update project_euler/problem_47/sol1.py
MarkMoretto c3d3887
Update project_euler/problem_47/sol1.py
MarkMoretto 97bcaaa
Update project_euler/problem_47/sol1.py
MarkMoretto 1c2ef0b
Update project_euler/problem_47/sol1.py
MarkMoretto 3cdc62c
Update project_euler/problem_47/sol1.py
MarkMoretto 2df1f5a
Update sol1.py
MarkMoretto def54a9
Update sol1.py
MarkMoretto 9603f48
Trailing whitespace
cclauss 7443e13
Update sol1.py
cclauss b148bec
Update __init__.py
cclauss 3b7e0b0
Update sol1.py
cclauss fe30329
Update __init__.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
""" | ||
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: | ||
""" | ||
Find unique prime factors of an integer. | ||
Tests include sorting because only the set really matters, | ||
not the order in which it is produced. | ||
>>> sorted(set(unique_prime_factors(14))) | ||
[2, 7] | ||
>>> set(sorted(unique_prime_factors(644))) | ||
[2, 7, 23] | ||
>>> set(sorted(unique_prime_factors(646))) | ||
[2, 17, 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 | ||
def upf_len(num: int) -> int: | ||
""" | ||
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]) | ||
False | ||
>>> equality([2, 2, 2, 2]) | ||
True | ||
>>> equality([1, 2, 3, 2, 1]) | ||
True | ||
""" | ||
return len(set(iterable)) in (0, 1) | ||
|
||
|
||
def run(n: int) -> list: | ||
""" | ||
Runs core process to find problem solution. | ||
>>> run(3) | ||
[644, 645, 646] | ||
""" | ||
|
||
# Incrementor variable for our group list comprehension. | ||
# This serves as the first number in each list of values | ||
# to test. | ||
base = 2 | ||
|
||
while True: | ||
# Increment each value of a generated range | ||
group = [base + i for i in range(n)] | ||
|
||
# Run elements through out unique_prime_factors function | ||
# Append our target number to the end. | ||
checker = [upf_len(x) for x in group] | ||
checker.append(n) | ||
|
||
# If all numbers in the list are equal, return the group variable. | ||
if equality(checker): | ||
return group | ||
|
||
# Increment our base variable by 1 | ||
base += 1 | ||
|
||
|
||
def solution(n: int = 4) -> int: | ||
"""Return the first value of the first four consecutive integers to have four | ||
distinct prime factors each. | ||
>>> solution() | ||
134043 | ||
""" | ||
results = run(n) | ||
return results[0] if len(results) else None | ||
|
||
|
||
if __name__ == "__main__": | ||
print(solution()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.