0

I am trying to iterate through some number as 02100021, this is a routing number which needs to get validated if it is a proper routing number. Therefore I am using the ABA routing number validation check as 3*(d1+d4+d7) + 7*(d2+d5+d8) + (d3+d6+d9) where mod 10 = 0

Since I have the number in integer type, my first question is how can I iterate through such number with 0's in it, or if there is some easier way of iterating through mode and multiplying it with such number.

Thanks

1 Answer 1

3

If I understand correctly, 7 * (d2, d5, d8) should be 7 * (d2 + d5 + d8). Zeros modulo 10 do not count.

bool correct(long x) { long a = x / 10 + x / 10000 + x / 10000000L; long b = x / 100 + x / 100000L + x / 100000000L; long c = x / 1000 + x / 1000000L + x / 1000000000L; int aba_checksum = (int)((3 * a + 7 * b + c) % 10); return aba_checksum == 0; } 

Corrected for d[i] = digit * 10 ^ (9 - i)

bool correct(long x) { long a = x / 100000000L + x / 100000L + x / 100; long b = x / 10000000L + x / 10000L + x / 10; long c = x / 1000000L + x / 1000L + x; int aba_checksum = (int)((3 * a + 7 * b + c) % 10); return aba_checksum == 0; } 

Modulo arithmetic can be done at the end. To prevent overflow (to negative numbers) one might do it earlier, above even 7 * b does not overflow.

Sign up to request clarification or add additional context in comments.

6 Comments

I think this doesn't work, as you don't mask off x's previous divisions (i.e. values greater than 10 after the division). (x/decimal_place) % 10 would fix that I believe. -- Untested.
x / 1000 + x / 1000000L + x / 1000000000L does not give you the sum of digits 3, 6 and 9. For x=123456789 it gives 123456+123+0=123579, not 3+6+9=18.
That's correct, I just tried that and the checksum is not coming properly.
Modulo 10 addition can be done at the end. My problem was, is d[i] = digit*10^(i - 1) or is d indexed from the other side (left to right).
Then one needs the number of digits (9 it seems). d1 = x / 1_000_000_000L.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.