Validating U.S. Bank Routing Numbers
In the U.S., banks use routing numbers to ensure checks and electronic transactions go to the correct institution. Each number is 9 digits in length with the 9th being a check digit. The algorithm to validate a routing number is
(3(d1?+?d4?+?d7) + 7(d2?+?d5?+?d8) + (d3?+?d6?+?d9)) mod 10 = 0
This essentially means we multiply each digit by its weight, sum the result, and verify that the modulo 10 result is zero, where modulo 10 is the remainder after dividing a number by 10.
MS Excel
We can represent the weight assigned to the digits as 371371371. Using pseudocode the MS Excel formula becomes
=MOD(SUMPRODUCT(individual_digits_in_weight, individual_digits_in_routing_number), 10)=0
With the routing number in cell B3, the formula is
=LET(_rtn,B3,_weight,371371371,
? ? _seq,SEQUENCE(LEN(_weight)),
? ? IF(LEN(_weight)<>LEN(_rtn),NA(),
? ? ? ? MOD(SUMPRODUCT(--MID(_weight,_seq,1),--MID(_rtn,_seq,1)),10))=0)
or as a lambda:
=LAMBDA(_rtn,
? ? LET(_weight,371371371,_seq,SEQUENCE(LEN(_weight)),
? ? ? ? IF(LEN(_weight)<>LEN(_rtn),NA(),
? ? ? ? ? ? MOD(SUMPRODUCT(--MID(_weight,_seq,1),--MID(_rtn,_seq,1)),10))=0))
Python
The Python solution is structurally similar
def isValidRoutingNumber(routingNumber):
? ? weight='371371371'
? ? return((sum([int(x)*int(y) for (x,y) in zip(routingNumber,weight)]) % 10)==0)
isValidRoutingNumber('111000025')
For more on routing numbers see ABA routing transit number - Wikipedia