Largest odd divisor Game to check which player wins | Function to find the Largest Odd Divisior Game to check which player wins ; Check if n == 1 then player 2 will win ; Check if n == 2 or n is odd ; While n is greater than k and divisible by 2 keep incrementing tha val ; Loop to find greatest odd divisor ; Check if n is a power of 2 ; Check if cnt is not one then player 1 wins ; Driver Code
function findWinner ( n , k ) { let cnt = 0 ; if ( n == 1 ) document . write ( " " ) ; else if ( ( n & 1 ) != 0 n == 2 ) document . write ( " " ) ; else { let tmp = n ; let val = 1 ; while ( tmp > k && tmp % 2 == 0 ) { tmp /= 2 ; val *= 2 ; } for ( let i = 3 ; i <= Math . sqrt ( tmp ) ; i ++ ) { while ( tmp % i == 0 ) { cnt ++ ; tmp /= i ; } } if ( tmp > 1 ) cnt ++ ; if ( val == n ) document . write ( " " ) ; else if ( n / tmp == 2 && cnt == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } } let n = 1 , k = 1 ; findWinner ( n , k ) ;