Counter game

Sort by

recency

|

206 Discussions

|

  • + 0 comments

    c++

    string counterGame(long n) {
    
     std::bitset<sizeof(long) * 8> bits = n-1;
     
     if(bits.count() % 2 == 0){
        return "Richard";
     }
     return "Louise";
    }
    
  • + 0 comments

    Javascript Solution

    function counterGame(n) {
      function checkBase(number) {
        const base = Math.log(number) / Math.log(2);
        if (Number.isInteger(base)) {
          return  number / 2;
        } else {
          const closestBase = Math.floor(base);
          return number - 2 ** closestBase;
        }
      }
    
      let counter = 0;
    
      while (n > 1) {
        n = checkBase(n);
        counter++;
      }
    
      if (counter % 2 === 1) {
        return "Louise";
      } else {
        return "Richard";
      }
    }
    
  • + 0 comments

    Typescript solution

    function counterGame(n: number): string {
        let turn = true
        // Write your code here
        const isPowerOfTwo = (num:number):{isPower: boolean, lowerPower:number} => {
            let calc = 1
            let power = 0
            while(calc < num){
                power += 1
                calc *=2
            }
            if(calc == num){
                return {
                    isPower: true,
                    lowerPower: 0
                }
            } else {
                return {
                    isPower: false,
                    lowerPower: calc/2 
                }
            }
        }
        while(n != 1){
            const isPowtwo = isPowerOfTwo(n)
            if(isPowtwo.isPower){
                n = n/2
            } else {
                n -= isPowtwo.lowerPower
            }
            turn = !turn
        }
    return turn?'Richard': 'Louise';
    
    }
    
  • + 0 comments

    public static String counterGame(long n) { int bits = Long.bitCount(n); int trailingZeros = Long.numberOfTrailingZeros(n);

        return (bits - 1 + trailingZeros) % 2 == 0 ? "Richard" : "Louise";
    

    } `

  • + 0 comments

    Python

    def counterGame(n):
        pop_count = bin(n).count('1')
        zero_count = (n & -n).bit_length() - 1
        if (pop_count + zero_count) % 2 == 0:
            return "Louise"  
        else:
            return "Richard"