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.
- Counter game
- Discussions
Counter game
Counter game
Sort by
recency
|
180 Discussions
|
Please Login in order to post a comment
public static String counterGame(long n) { int counter = 0; while(n > 1) { int power =(int)(Math.log(n) / Math.log(2)); if(Math.pow(2,power) != n) n = n - (long)Math.pow(2,power); else n = n/2; counter++; } return (counter % 2 == 0) ? "Richard" : "Louise"; }
` TypeScript function counterGame(n: bigint): string { //count 0s up to first 1 of binary representation let trailingZeros = 0; while((n & 1n) === 0n) {
} `
Edit: lot of edits because formatting broken
The framework code for Javascript/TypeScript is incorrect here as the question allows n up to (2^64)-1 and Javascript's number format only allows up to (2^53)-1 as an integer. However it seems none of the test cases are actually using values this large as they all passed without changing this.
JavaScript number will still cause issues if any bitshifts are used as those only work up to (2^32)-1.
All works properly if we just use BigInts.
Java. Not the best solution but, here we go.
Python 3