-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added solution to Project Euler problem 301 #3343
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 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
693f6f0
Added solution to Project Euler problem 301
fb35f22
Added newline to end of file
abf1b28
Merge pull request #1 from TheAlgorithms/master
e04b91f
Fixed formatting and tests
5bd0dcd
Merge branch 'master' of https://github.com/KumarUniverse/Python
4a08a56
Changed lossCount to loss_count
a078c48
Merge pull request #2 from TheAlgorithms/master
c902c36
Fixed default parameter value for solution
e920501
Removed helper function and modified print stmt
96e6a1f
Fixed code formatting
a42997e
Optimized solution from O(n^2) to O(1) constant time
0cbfa27
Update sol1.py
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
Empty file.
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,75 @@ | ||
""" | ||
Project Euler Problem 301: https://projecteuler.net/problem=301 | ||
|
||
Problem Statement: | ||
Nim is a game played with heaps of stones, where two players take | ||
it in turn to remove any number of stones from any heap until no stones remain. | ||
|
||
We'll consider the three-heap normal-play version of | ||
Nim, which works as follows: | ||
- At the start of the game there are three heaps of stones. | ||
- On each player's turn, the player may remove any positive | ||
number of stones from any single heap. | ||
- The first player unable to move (because no stones remain) loses. | ||
|
||
If (n1, n2, n3) indicates a Nim position consisting of heaps of size | ||
n1, n2, and n3, then there is a simple function, which you may look up | ||
or attempt to deduce for yourself, X(n1, n2, n3) that returns: | ||
- zero if, with perfect strategy, the player about to | ||
move will eventually lose; or | ||
- non-zero if, with perfect strategy, the player about | ||
to move will eventually win. | ||
|
||
For example X(1,2,3) = 0 because, no matter what the current player does, | ||
the opponent can respond with a move that leaves two heaps of equal size, | ||
at which point every move by the current player can be mirrored by the | ||
opponent until no stones remain; so the current player loses. To illustrate: | ||
- current player moves to (1,2,1) | ||
- opponent moves to (1,0,1) | ||
- current player moves to (0,0,1) | ||
- opponent moves to (0,0,0), and so wins. | ||
|
||
For how many positive integers n <= 2^30 does X(n,2n,3n) = 0? | ||
""" | ||
|
||
|
||
def x(n: int, n2: int, n3: int) -> int: | ||
""" | ||
Returns: | ||
- zero if, with perfect strategy, the player about to | ||
move will eventually lose; or | ||
- non-zero if, with perfect strategy, the player about | ||
to move will eventually win. | ||
|
||
>>> x(1, 2, 3) | ||
0 | ||
>>> x(3, 6, 9) | ||
12 | ||
>>> x(8, 16, 24) | ||
0 | ||
>>> x(11, 22, 33) | ||
60 | ||
>>> x(1000, 2000, 3000) | ||
3968 | ||
""" | ||
return n ^ n2 ^ n3 | ||
|
||
|
||
def solution(n: int = 2 ** 30) -> int: | ||
""" | ||
For a given integer n <= 2^30, returns how many Nim games are lost. | ||
>>> solution(2) | ||
2 | ||
>>> solution(2 ** 10) | ||
144 | ||
""" | ||
loss_count = 0 | ||
for i in range(1, n + 1): | ||
if x(i, 2 * i, 3 * i) == 0: | ||
loss_count += 1 | ||
|
||
return loss_count | ||
|
||
|
||
if __name__ == "__main__": | ||
print(solution()) | ||
KumarUniverse marked this conversation as resolved.
Show resolved
Hide resolved
|
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.