1

I was wondering if its possible in constant time to calculate the above mentioned goal. I need it to solve a problem on codechef.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Harshit Dubey
  • 13
  • 1
  • 2
  • 1
    I’m voting to close this question because it's a mathematics question, and as such belongs on the Math Stack Exchange. – gspr May 09 '21 at 21:08
  • @gspr How to compute something in constant time (if possible) is a question relevant to programming. The boundary between mathematics and computer science isn't sharp. – John Coleman May 09 '21 at 21:11
  • 1
    @JohnColeman: I agree that the line between mathematics and CS is fuzzy. It is definitely an open question as to whether this question best fits the math or the CS stack exchanges, but I don't see how it belongs on SO. – gspr May 10 '21 at 11:02

1 Answers1

1

It is impossible to compute gcd(a+k,b+k) in constant time knowing only gcd(a,b).

Suppose that c,d are any two natural numbers with d < c.

Let

a = d - d = 0
b = c - d
k = d

Then we know in O(1) time that

gcd(a,b) = gcd(0, c - d) = c - d

If we could compute gcd(a+k,b+k) = gcd(c,d) in O(1) additional time, then we could compute all gcds in O(1) time, which is impossible.

Having said all that, it is of course possible that in some cases of interest, knowledge of gcd(a,b) could lead to faster computation of gcd(a+k,b+k) than would otherwise be possible.

John Coleman
  • 51,337
  • 7
  • 54
  • 119