algorithm - Find a pair of intergers that can be reached to another pair using 4 operations -


given pair of integers (eg. (x,y)). want find whether possible convert them pair of integers using 4 operations, mentioned below, @ time number of times. operations follows:

(x,x+y) or (x+y,y) or (x-y,y) or (x,x-y) 

for eg. (4,2) can converted (2,6) doing following operations:

(x-y,y) --- (2,2) (x,x+y) --- (2,4) (x,x+y) --- (2,6) 

where (2,2) cannot converted (4,4). answer should yes or no.

claim: (x, y) can reach (z, w) if , if gcd(x, y) = gcd(z, w).

proof: (necessary) gcd(x, y) = gcd(x, x + y) = gcd(x + y, y) = gcd(x - y, y) = gcd(x, x - y). (sufficient) reachability symmetric. run euclidean algorithm reach (gcd(x, y), 0) (x, y).


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -