2
$\begingroup$

Given that:

  1. $91 \le a \le 100$

  2. $a + ( 10 * b ) = c $

  3. $a$ and $b$ both are int ( not float values. ie. The values are not fraction )

  4. Value of $c$ is any random value (eg. $108$)

What's the value of $a$ and $b$ for $c$ (eg. $c = 108$)?

(For example, by guessing, I got $a = 98$ and $b = 1$).

But I just want to come up with some single line mathematical formula, not a for-loop based algorithm that checks values one by one.

2 Answers 2

2

Let us define $d=a-91$, so $0 \le d \le 9$.
$d=(c-91) \pmod {10}$

$b=\lfloor \frac {c-91}{10} \rfloor=\frac {c-a}{10}$

$a=d+91$

Basically, $d$ is the ones digit of $c-91$ and $b$ is all the rest of the digits.

  • 0
    @VishwasGagrani: forgot the floor function. Fixed now. You can also subtract $d$ which will give a multiple of $10$2012-09-06
2

Say b>= 0 and if a = 91, then c can be, 91, 101, 111,... if a = 92, c can be, 92,102...

Similarly for others, so if c mod 10 = x; then a = 9x.

so b = (c - a)/10 is integer since, c,a has same digit in units place.