Power of 2 game code in python
This can actually be solved with a super efficient 1 line of code, but the explanation is long :) Show Your code will fail with any input bigger than \$2^{62}\$ because you never overshoot this number by multiplying by 2 a power of 2 (in your code: Once you detect that a number is a power of 2 you shouldn't reset
A thing you could use to your advantage is the way powers of 2 look in binary code:
Notice how all bits are 0 except one, the position of this 1 bit tells you if you need an odd or even number of divisions (turns in the game) to reach 1.
As soon as you reach a power of 2, this check is enough to tell you who's gonna win. An odd number of turns left means that the player whose turn it is now will win, otherwise the other player will win. We can go further with looking at the bits and bypass the need to find the next lower power of 2 to divide
When the number isn't a power of 2, according to the game you need to remove the next lower power of 2. In binary it's gonna look like removing the left most bit (you can look at the numbers above to confirm this). Given our knowledge and solutions so far, we can rewrite the rules of the game to simple binary operations:
This means the number of turns until we reach a power of 2 and find the answer depends on how many 1-bits are in the number, and knowing if this number is odd or even - combined with the answer to whether the power you reach is odd or even - is all you really need to know to predict the outcome of the game! Let's test this solution: here's what you see when you track the changes in a number as the game is played, in binary:
There are 4 Here is the most optimized code I could possibly write to solve this:
This code looks at each bit once, so the time complexity of this code is Judging by the fact that the exception to the rules (where Richard wins when Louise gets the 1 at the start of the game) emerges naturally with this solution if you don't add an if statement at the start to take care of it, I'd say this is what the author of this challenge expected to be the best possible answer. BonusThere are processor instructions for counting
EDIT: a shorter code suggested by @JS1:
How do you code a power of 2 in Python?Python has three ways to exponentiate values:. The ** operator. To program 25 we do 2 ** 5 .. The built-in pow() function. 23 coded becomes pow(2, 3) .. The math. pow() function. To calculate 35, we do math. pow(3, 5) .. How do you code a power of 2?Keep dividing the number by two, i.e, do n = n/2 iteratively until n becomes 1. In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2.
How do you code a power in Python?The ** operator in Python is used to raise the number on the left to the power of the exponent of the right. That is, in the expression 5 ** 3 , 5 is being raised to the 3rd power.
What number is power of 2?1, 2, 4, 8, 16, 32, 64, 128, 256, 512, ... (sequence A000079 in the OEIS) Because two is the base of the binary numeral system, powers of two are common in computer science. Written in binary, a power of two always has the form 100...000 or 0.00...
|