CMPSCI 250 Discussion #2: Playing With Numbers

David Mix Barrington

27/29 September 2004

Solution for this discussion:

Working with prime numbers and divisibility gives rise to a number of computational problems: Does this natural divide that one? Is this natural prime? If not, what are its factors? Before the days of hand calculators, various computational tricks were developed to solve some of these by hand or even mentally. In this Excursion we'll learn some of these tricks, and use them to solve some problems about primality. The justifications for these tricks are in Section 3.2 of the text.

Last Digit: n is divisible by 2 if and only if its last digit is in {0,2,4,6,8}. It is divisible by 5 if and only if the last digit is in {0,5}.

Last Two Digits: Test divisibility by 4 or 25.

Last Three Digits: Test divisibility by 8 or 125, etc.

Sum of Digits: Test divisibility by 9 and thus by 3.

Sum of Pairs of Digits: Test divisibility by 99 and thus by 11.

Sum of Triples of Digits: Test divisibility by 999 and thus by 37.

Example: 17256 = 8 * 3 * 719. Is 719 prime? We must test divisibility by all primes up to the square root of 719, which is less than 27. Thus we must test 7, 11, 13, 17, 19, and 23.

Alternating Sum of Digits: 17256 is congruent to 1 - 7 + 2 - 5 + 6 = -3 modulo 11.

Alternating Sum of Pairs of Digits: Test for 101, if you care.

Alternating Sum of Triples of Digits: Test for 1001 = 7 * 11 * 13. For example, 17256 is congruent to -17 + 256 = 239 modulo 7, 11, or 13.

Here is one more general trick for testing divisibility by one of the small primes for which the above tricks don't work (or still leave you with a three-digit number to test). Remember that in carrying out the division, you don't care about the quotient, only whether the remainder is zero. So you make progress if you can convert the given natural to a smaller natural such that the two numbers are either both divisible by the prime or both not divisible by it. A quick way to do this mentally is to first add or subtract a multiple of the prime to get a number ending in 0, then remove the 0. Clearly adding or subtracting the multiple gives you something congruent modulo the prime. Dividing by ten won't give you something congruent, but as long as the prime in question isn't two or five, it will not change the divisibility of the number by the prime (the reason for this will be clearer later in the chapter).

So to test 719 for divisibility by 7, for example, we add 21 to get 740, divide by ten to get 74, whereupon if we know our multiplication tables we are done because 7 does not divide 74. In this way we can soon verify that 719 is in fact prime. We checked 11, and to do 13 we can subtract 3 * 13 = 39 from 719 to get 680, and note that 13 does not divide 68. For 17, we may add 3 * 17 = 51 to 719 getting 770, and 17 does not divide 77 (which is clearly 7 * 11). 19 is even easier, as subtracting 19 leaves 700, and 19 certainly doesn't divide 7. Finally for 23 we subtract 3 * 23 = 69 from 719 leaving 650, and 23 does not divide 65. Since the next prime, 29, has a square bigger than 719, we are done and have shown that 719 is prime.

Writing Exercise:

Using the tricks described above without a calculator and showing your work clearly, find:

Last modified 29 September 2004