[Math] Calculating the modulus for a large value

modular arithmetic

I'm building a calculator for International Bank Account Numbers (IBAN) but I'm having trouble calculating the modulus for a big number. That is to say, my results are not the same as that of the examples I have.

To calculate the IBAN checksum I have to follow these steps (taken from WikiPedia):

  1. Replace the two check digits by 00 (e.g., GB00 for the UK).
  2. Move the four initial characters to the end of the string.
  3. Replace the letters in the string with digits, expanding the string as necessary, such that A or a=10, B or b=11 and Z or z=35. Each alphabetic character is therefore replaced by 2 digits.
  4. Convert the string to an integer (i.e., ignore leading zeroes).
  5. Calculate Mod-97 of the new number.
  6. Subtract the remainder from 98 and, if necessary, pad with a leading 0 to make a two digit number.

For example NL20INGB0001234567

  1. NL00INGB0001234567
  2. INGB0001234567NL00
  3. 182316110001234567232100
  4. 182316110001234567232100
  5. 182316110001234567232100 % 97 = 67
  6. 98 – 67 = 31

Since 31 does not equal 20 I conclude that something went wrong. According to an example 182316110001234567232100 % 97 should yield 78 but I don't see how.

What am I doing wrong in my modulus calculation?

Cheers

Best Answer

Indeed 182316110001234567232100 mod 97 = 78. Perhaps you're using too small an integer type. 182316110001234567232100 requires at least 78 (unsigned) bits.

Related Question