-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Created problem_46 in project_euler #2343
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 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
545b513
Create __init__.py
Kush1101 b4f99cf
Add files via upload
Kush1101 f1eb03a
Update sol1.py
Kush1101 fbde796
Update sol1.py
Kush1101 d41666f
Update project_euler/problem_46/sol1.py
Kush1101 724503c
Update project_euler/problem_46/sol1.py
Kush1101 361b8d2
Update sol1.py
Kush1101 84db6de
exact
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,86 @@ | ||
""" | ||
It was proposed by Christian Goldbach that every odd composite number can be | ||
written as the sum of a prime and twice a square. | ||
|
||
9 = 7 + 2 × 12 | ||
15 = 7 + 2 × 22 | ||
21 = 3 + 2 × 32 | ||
25 = 7 + 2 × 32 | ||
27 = 19 + 2 × 22 | ||
33 = 31 + 2 × 12 | ||
|
||
It turns out that the conjecture was false. | ||
|
||
What is the smallest odd composite that cannot be written as the sum of a | ||
prime and twice a square? | ||
""" | ||
|
||
from typing import List | ||
|
||
seive = [True] * 100001 | ||
i = 2 | ||
while i * i <= 100000: | ||
if seive[i]: | ||
for j in range(i * i, 100001, i): | ||
seive[j] = False | ||
i += 1 | ||
|
||
|
||
def is_prime(n: int) -> bool: | ||
""" | ||
Returns True if n is prime, | ||
False otherwise, for 2 <= n <= 100000 | ||
>>> is_prime(87) | ||
False | ||
>>> is_prime(23) | ||
True | ||
>>> is_prime(25363) | ||
False | ||
""" | ||
return seive[n] | ||
|
||
|
||
odd_composites = [num for num in range(3, len(seive), 2) if not is_prime(num)] | ||
|
||
|
||
def compute_nums(n: int) -> List[int]: | ||
""" | ||
Returns a list of first n odd composite numbers which do | ||
not follow the conjecture. | ||
>>> compute_nums(1) | ||
[5777] | ||
>>> compute_nums(2) | ||
[5777, 5993] | ||
>>> compute_nums(0) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: n must be => 0 | ||
>>> compute_nums("a") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: n must be an exact integer | ||
>>> compute_nums(1.1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: n must be an exact integer | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
if not isinstance(n, int): | ||
raise ValueError("n must be an exact integer") | ||
if n <= 0: | ||
raise ValueError("n must be => 0") | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
list_nums = [] | ||
for num in range(len(odd_composites)): | ||
i = 0 | ||
while 2 * i * i <= odd_composites[num]: | ||
rem = odd_composites[num] - 2 * i * i | ||
if is_prime(rem): | ||
break | ||
i += 1 | ||
else: | ||
list_nums.append(odd_composites[num]) | ||
if len(list_nums) == n: | ||
return list_nums | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{compute_nums(1) = }") |
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.