We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defcounterGame(n):turns=0whilen>1:# Check if n is a power of 2if(n&(n-1))==0:# If it's a power of 2, divide by 2n>>=1#Equivalentton=n// 2else:# If not a power of 2, find the largest power of 2 less than n# This can be found by taking 2 raised to the power of (n.bit_length() - 1)# n.bit_length() gives the number of bits required to represent n,# which is equivalent to floor(log2(n)) + 1.# So, the largest power of 2 less than n is 2^(floor(log2(n))).# We can get this using a bit shift: 1 << (n.bit_length() - 1)largest_power_of_2=1<<(n.bit_length()-1)n-=largest_power_of_2turns+=1# If the number of turns is odd, Louise wins (since she starts)# If the number of turns is even, Richard winsifturns%2==1:return"Louise"else:return"Richard"
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Counter game
You are viewing a single comment's thread. Return to all comments →