-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Created problem_45 in project_euler and Speed Boost for problem_34/sol1.py #2349
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 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
34d4d13
Create __init__.py
Kush1101 ffdfc27
Add files via upload
Kush1101 79afd63
Update sol1.py
Kush1101 0c4638e
Update sol1.py
Kush1101 4bb67a0
Update project_euler/problem_45/sol1.py
Kush1101 2b6c4ff
Update sol1.py
Kush1101 2b581f7
Update sol1.py
Kush1101 42c1887
Update project_euler/problem_34/sol1.py
Kush1101 136b05a
Update project_euler/problem_34/sol1.py
Kush1101 b68a48b
Update sol1.py
Kush1101 f38caae
Update project_euler/problem_34/sol1.py
Kush1101 3b8fd2f
Update sol1.py
Kush1101 ae70537
Update project_euler/problem_34/sol1.py
Kush1101 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
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,57 @@ | ||
""" | ||
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: | ||
Triangle T(n) = (n * (n + 1)) / 2 1, 3, 6, 10, 15, ... | ||
Pentagonal P(n) = (n * (3 * n − 1)) / 2 1, 5, 12, 22, 35, ... | ||
Hexagonal H(n) = n * (2 * n − 1) 1, 6, 15, 28, 45, ... | ||
It can be verified that T(285) = P(165) = H(143) = 40755. | ||
|
||
Find the next triangle number that is also pentagonal and hexagonal. | ||
All trinagle numbers are hexagonal numbers. | ||
T(2n-1) = n * (2 * n - 1) = H(n) | ||
So we shall check only for hexagonal numbers which are also pentagonal. | ||
""" | ||
|
||
|
||
def hexagonal_num(n: int) -> int: | ||
""" | ||
Returns nth hexagonal number | ||
>>> hexagonal_num(143) | ||
40755 | ||
>>> hexagonal_num(21) | ||
861 | ||
>>> hexagonal_num(10) | ||
190 | ||
""" | ||
return n * (2 * n - 1) | ||
|
||
|
||
def is_pentagonal(n: int) -> bool: | ||
""" | ||
Returns True if n is pentagonal, False otherwise. | ||
>>> is_pentagonal(330) | ||
True | ||
>>> is_pentagonal(7683) | ||
False | ||
>>> is_pentagonal(2380) | ||
True | ||
""" | ||
root = (1 + 24 * n) ** 0.5 | ||
return ((1 + root) / 6) % 1 == 0 | ||
|
||
|
||
def compute_num(start: int = 144) -> int: | ||
""" | ||
Returns the next number which is traingular, pentagonal and hexagonal. | ||
>>> compute_num(144) | ||
1533776805 | ||
""" | ||
n = start | ||
num = hexagonal_num(n) | ||
while not is_pentagonal(num): | ||
n += 1 | ||
num = hexagonal_num(n) | ||
return num | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{compute_num(144)} = ") |
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.