Check if possible to move from given coordinate to desired coordinate | javascript program to check if it is possible to reach ( a , b ) from ( x , y ) . Returns GCD of i and j ; Returns true if it is possible to go to ( a , b ) from ( x , y ) ; Find absolute values of all as sign doesn 't matter. ; If gcd is equal then it is possible to reach . Else not possible . ; Converting coordinate into positive integer
function gcd ( i , j ) { if ( i == j ) return i ; if ( i > j ) return gcd ( i - j , j ) ; return gcd ( i , j - i ) ; } function ispossible ( x , y , a , b ) { x = Math . abs ( x ) ; y = Math . abs ( y ) ; a = Math . abs ( a ) ; b = Math . abs ( b ) ; return ( gcd ( x , y ) == gcd ( a , b ) ) ; } var x = 35 , y = 15 ; var a = 20 , b = 25 ; if ( ispossible ( x , y , a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;