0

If I have an equation like (x+c) mod y = z and I need to solve for y, how would I go about doing that?

Apologies if this is more math than programming.

display name
  • 43
  • 1
  • 1
  • 4
  • 2
    Not possible in general, at least not uniquely. – John Coleman Apr 01 '17 at 19:07
  • Does this mean you know the values of `x,c,z` and want to find `y`? What if there are no solutions or multiple solutions (both of which can happen)? Are all those values to be positive integers? Do you want an algorithm to find all solutions, a math formula, or something else? Finally, what work have you done on this problem and just where are you stuck? – Rory Daulton Apr 01 '17 at 19:09
  • Yes, x , c and z are constants (positive integers). I'm looking for a formula – display name Apr 01 '17 at 19:10
  • 1
    Under those conditions, `y` is any divisor of `x + c - z` that is greater than `z`. This can give no, one, or many answers. Note that all positive integers divide zero, so you may have infinitely many answers. – Rory Daulton Apr 01 '17 at 19:12

1 Answers1

1

No simple formula exists. If a mod n = r then n divides a-r and 0 <= r < n. Candidate n can be found by factoring a - r and finding divisors which are larger than r. Factoring is a much-studied but non-trivial problem. Pick your favorite factoring algorithm. Unless a-r is prime, there won't be a unique solution (unless r is larger than any proper-divisor of a-r).

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