1

How to check that for given natural numbers "a" and " b", a digit at a position "i" from number "b" is less than the digit at position "i" at number "a" without iterating digit by digit?

e.g.

for :

a = 22222

b = 10001 is ok but b= 10030 is not ok.

I know that in case digits are less or equal then the subtraction can be done without "borrowing" but how can I validate if a subtraction was done with "borrowing" or not ?

Clyde
  • 11
  • use $\mod{}$ function followed by integer division and comparison. For example if you want to compare 22222 and 10030 at the position where the 3 occurs, first take $\mod 100$ so you have 22 and 30. Next integer divide by 10 (ignore the remainders) so now you have 2 and 3. Compare them – Mirko Jul 28 '15 at 20:58
  • Or compare the digits after dividing by $10^{i-1}$ and taking $\mod{10}$. I don't know if one of the two approaches is more efficient than the other, but I doubt that it would make a difference unless the numbers involved are quite large. – A.P. Jul 28 '15 at 21:07
  • Thanks, that is what i am using now. I am taking pow(10,n) where n is length of number -1 and going less a power o 10 each iteration. It works, but I was more thinking if there is an approach that would avoid going digit by digit. But yeah, this is still good I guess. – Clyde Jul 29 '15 at 11:08
  • The mod solution seems to be the only one I guess. I'd appreciate if you suggest it as official answer, it worked for me. – Clyde Aug 05 '15 at 16:44

1 Answers1

0

use mod function followed by integer division and comparison. For example if you want to compare 22222 and 10030 at the position where the 3 occurs, first take mod100mod100 so you have 22 and 30. Next integer divide by 10 (ignore the remainders) so now you have 2 and 3. Compare them >> by Mirko

Clyde
  • 11