Two Balls Reachability Game | Recursive function to return gcd of a and b ; Function returns if it 's   possible to have X white   and Y black balls or not. ; Finding gcd of ( x , y ) and ( a , b ) ; If gcd is same , it 's always  possible to reach (x, y) ; Here it 's never possible  if gcd is not same ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function IsPossible ( a , b , x , y ) { let final = gcd ( x , y ) ; let initial = gcd ( a , b ) ; if ( initial == final ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } let A = 1 , B = 2 , X = 4 , Y = 11 ; IsPossible ( A , B , X , Y ) ; A = 2 , B = 2 , X = 3 , Y = 6 ; IsPossible ( A , B , X , Y ) ;