5

I have a mathematical problem that is part of my programming problem

I have a statement like

a = b%30;

How can I calculate b in terms of a?

I gave it a thought but couldn't figure it out.

arshajii
  • 127,459
  • 24
  • 238
  • 287
Old Person
  • 99
  • 1
  • 3
  • 6

2 Answers2

7

By definition,

b == 30*n + a

for some integer n.

Note that there are multiple b values that can give you the same a:

>>> b = 31
>>> b % 30
1
>>> b = 61
>>> b % 30
1
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • so there isn't a specific way to go about determining value of b using a in this case right? – Old Person Jan 03 '14 at 22:23
  • If `%` is the remainder operator as found in most programming languages, then your solution is incorrect for `a` between -29 and -1. Even if it is the modulus, your solution `b == 30*n + a` does not work for a>=30. – Pascal Cuoq Jan 03 '14 at 22:23
  • @PascalCuoq Presumably `a` will always be in the range `[0,29]`. – arshajii Jan 03 '14 at 22:25
  • @ShenoyTinny If you have no further information about `a` and `b`, then you can't determine their unique values. – arshajii Jan 03 '14 at 22:26
  • Why presume when it is so simple to give the shape of solutions for all values of `a`? – Pascal Cuoq Jan 03 '14 at 22:31
  • @PascalCuoq Because `a` is assigned to `b%30`, which cannot take on any values besides those in the aforementioned range. – arshajii Jan 03 '14 at 22:33
  • @arshajii In “math” (the question is tagged “math”), the `=` sign denotes equality, not assignment. In most programming languages, `a` can be negative after executing the equivalent of the C assignment `a = b % 30;` – Pascal Cuoq Jan 03 '14 at 22:37
  • @PascalCuoq Yes, some languages can give you an `a` in the range `[-29, 29]`; however the solution above is still valid in this range. The `=` sign here almost certainly denotes assignment, as is evidenced by the semicolon and *"I have a statement like"*. – arshajii Jan 03 '14 at 22:42
  • @arshajii “the solution above is still valid in this range” No it is not. – Pascal Cuoq Jan 03 '14 at 22:42
  • @PascalCuoq Do you have a counter-example? – arshajii Jan 03 '14 at 22:43
  • Counter-example: a = -1, n = 1, 30 * n + a = 29, 29 % 30 is not -1. – Pascal Cuoq Jan 03 '14 at 22:47
  • @PascalCuoq Well what is your `b`, in that case? If `b` is `-1`, for instance, then `a = -1` and `n` can be `0`. Why have you chosen `n = 1`? – arshajii Jan 03 '14 at 22:49
  • I applied your formula `b == 30*n + a` for a = -1, n = 1. It gave me b == 29, and the value 29 for b does not satisfy `b == 30*n + a`. What do you call a counter-example if it is not a triple of values for a, b and n that show that your formula does not work? – Pascal Cuoq Jan 03 '14 at 22:53
  • @PascalCuoq I think you misunderstood. I said *"for some integer `n`"* in that there exists some value for `n` (which we may not know) that will satisfy that formula. In your example, `n=1` may or may not work, but there will always be *some* `n` value that will work. This is by definition of the [modulus operation](http://en.wikipedia.org/wiki/Modulo_operation). Of course, if we are only given `a`, we cannot deduce `n` or `b`, since multiple `b` values can map to the same `a` value. – arshajii Jan 03 '14 at 22:57
3

First, obviously, there are in general several solutions for b for a given a.

If % is the remainder operator found in most programming languages, then the sign of a is critical. You know that this is a website for programming questions, right?

If |a|>=30, then there are no solutions

If a = 0, the solutions are the multiples of 30.

If 0 < a < 30, the solutions are all the b such that b = 30 * k + a for some positive integer k.

If -30 < a < 0, the solutions are all the b such that b = - (30 * k - a) for some positive integer k.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281