s
Section 5.6 The Mighty Mod Function
437
If a 5 occurs in the right half, the same binary code would be represented by
a bar of width 1, a space of width 2, a bar of width 3, and a space of width 4. In
addition, there are some extra bits inserted at each end and in the middle for a total
of 95 bits.
The check digit C (digit 12) is computed from the previous 11 digits as follows:
c3 ∙
5
i=0
a 2i+1 + ∙
5
i=1
a 2i + Cd mod 10 = 0
—that is, three times the sum of all the odd digits plus the sum of all the even digits
plus the check digit should give a multiple of 10. This formula is similar to the one
for the ISBN-13 check digit except that for the ISBN-13 it is the even digits that
are multiplied by 3 and for the bar code it is the odd digits.
example 57
The American Bankers Association (ABA) devised a code to represent a specific
financial institution. This number was originally designed for paper checks, and
it does appear on the bottom of paper checks, but today it is also used for such
transactions as direct deposit or electronic funds transfer. This routing number
consists of 8 digits plus a check digit. The first four digits represent Federal Reserve information connected with the financial institution and the second four
digits identify the institution itself. The check digit C is computed by
c3 ∙
2
i=0
a 3i+1 + 7 ∙
2
i=0
a 3i+2 + 1 ∙
1
i=0
a 3i+3 + Cd mod 10 = 0
—that is, digits 1, 4, and 7 are multiplied by 3 and added, digits 2, 5, and 8 are
multiplied by 7 and added, and digits 3 and 6 are multiplied by 1 and added. The
total plus the check digit should be a multiple of 10.
Generating and Decomposing Integers
The modulo function provides an easy way to generate integer values within some
range 0 through n − 1 for some positive integer n. Take any positive integer m and
compute m mod n. If you have a function to generate a random (or pseudorandom)
integer m, this process generates a random (or pseudorandom) integer within the
desired range. You may also want to cycle through the integers in this range in a
controlled fashion.
example 58
You want to show a sequence of five images on your Web page. The images are
stored in an arraylike structure as Image(1), …, Image(5), and the actual image
to be displayed is stored in the variable DisplayImage, which is initialized to
Image(1):
i = 1
DisplayImage = Image(i)
Section 5.6 The Mighty Mod Function
437
If a 5 occurs in the right half, the same binary code would be represented by
a bar of width 1, a space of width 2, a bar of width 3, and a space of width 4. In
addition, there are some extra bits inserted at each end and in the middle for a total
of 95 bits.
The check digit C (digit 12) is computed from the previous 11 digits as follows:
c3 ∙
5
i=0
a 2i+1 + ∙
5
i=1
a 2i + Cd mod 10 = 0
—that is, three times the sum of all the odd digits plus the sum of all the even digits
plus the check digit should give a multiple of 10. This formula is similar to the one
for the ISBN-13 check digit except that for the ISBN-13 it is the even digits that
are multiplied by 3 and for the bar code it is the odd digits.
example 57
The American Bankers Association (ABA) devised a code to represent a specific
financial institution. This number was originally designed for paper checks, and
it does appear on the bottom of paper checks, but today it is also used for such
transactions as direct deposit or electronic funds transfer. This routing number
consists of 8 digits plus a check digit. The first four digits represent Federal Reserve information connected with the financial institution and the second four
digits identify the institution itself. The check digit C is computed by
c3 ∙
2
i=0
a 3i+1 + 7 ∙
2
i=0
a 3i+2 + 1 ∙
1
i=0
a 3i+3 + Cd mod 10 = 0
—that is, digits 1, 4, and 7 are multiplied by 3 and added, digits 2, 5, and 8 are
multiplied by 7 and added, and digits 3 and 6 are multiplied by 1 and added. The
total plus the check digit should be a multiple of 10.
Generating and Decomposing Integers
The modulo function provides an easy way to generate integer values within some
range 0 through n − 1 for some positive integer n. Take any positive integer m and
compute m mod n. If you have a function to generate a random (or pseudorandom)
integer m, this process generates a random (or pseudorandom) integer within the
desired range. You may also want to cycle through the integers in this range in a
controlled fashion.
example 58
You want to show a sequence of five images on your Web page. The images are
stored in an arraylike structure as Image(1), …, Image(5), and the actual image
to be displayed is stored in the variable DisplayImage, which is initialized to
Image(1):
i = 1
DisplayImage = Image(i)
