Divisibility Tests and Factoring


First, I'll discuss quick ways for deciding whether a number is divisible by various small integers. In what follows, I'll assume that numbers are represented as strings of decimal digits (i.e. in base 10) as usual.

Proposition. (a) An integer is divisible by 2 if and only if its last digit is divisible by 2.

(b) An integer is divisible by 5 if and only if its last digit is 0 or 5.

Proof. The proofs for these two tests are nearly identical; I'll do the one for divisibility by 2 as an example.

Suppose the decimal representation of x is

$$x_n x_{n-1} \ldots x_2 x_1 x_0.$$

That is, $x_0$ is the units digit, $x_1$ is the tens digit, and so on. For 1728,

$$x_3 = 1, x_2 = 7, x_1 = 2, x_0 = 8.$$

Then

$$x = x_n\cdot 10^n + x_{n-1}\cdot 10^{n-1} + \cdots + x_1\cdot 10 + x_0.$$

Since $2 \mid 10$ , it follows that $2
   \mid 10^n$ for $n \ge 1$ . Thus, $2 \mid x$ if and only if $2 \mid x_0$ --- that is, x is even if and only if the units digit $x_0$ is even.

Definition. The digital sum of an integer n is the sum of the digits in the decimal representation of n.


Example. The digital sum of 1728 is 18:

$$1 + 7 + 2 + 8 = 18.$$

The digital sum of 278349 is 33:

$$2 + 7 + 8 + 3 + 4 + 9 = 33.\quad\halmos$$


Proposition. (a) An integer is divisible by 3 if and only if its digital sum is divisible by 3.

(b) An integer is divisible by 9 if and only if its digital sum is divisible by 9.

Proof. I'll prove the test for divisibility by 9 as an example. Suppose the decimal representation of x is

$$x_n x_{n-1} \ldots x_2 x_1 x_0.$$

Then

$$x = x_n\cdot 10^n + x_{n-1}\cdot 10^{n-1} + \cdots + x_1\cdot 10 + x_0.$$

The sum of the digits of x is

$$s = x_n + x_{n-1} + \cdots + x_1 + x_0.$$

Observe that

$$x - s = \left(x_n\cdot 10^n + x_{n-1}\cdot 10^{n-1} + \cdots + x_1\cdot 10 + x_0\right) - \left(x_n + x_{n-1} + \cdots + x_1 + x_0\right) =$$

$$x_n(10^n - 1) + x_{n-1}(10^{n-1} - 1) + \cdots + x_1(10 - 1).$$

The right side is divisible by 9, because

$$10 - 1 = 9, 10^2 - 1 = 99, 10^3 - 1 = 999, \quad\hbox{and so on.}$$

That is,

$$x - s = (\hbox{something divisible by 9}).$$

Hence, if $9 \mid x$ , then $9
   \mid s$ , and if $9 \mid s$ , then $9 \mid x$ .

Note that you can continue to sum the digits of the numbers that you get until you get something that is obviously divisible by 3 or 9. For example, start with 893948083:

$$8 + 9 + 3 + 9 + 4 + 8 + 0 + 8 + 3 = 52, \quad 5 + 2 = 7.$$

Since 7 is not divisible by 3 or by 9, neither is 52. Since 52 is not divisible by 3 or by 9, neither is 893948083.

The next result can be proved by a method similar to that used to prove the test for divisibility by 9. Since the proof is a little more complicated, I'll merely state the result and give an example.

Proposition. To test divisibility by 7, remove the last (units) digit, double it, and subtract it from the remainder of the number. The original number is divisible by 7 if and only if the result is divisible by 7.


Example. 9423242 is divisible by 2 and by 9. Here's how the divisibility by 7 test looks for this number:

$$942324 - 2\cdot 2 = 942320,$$

$$94232 - 2\cdot 0 = 94232,$$

$$9423 - 2\cdot 2 = 9419,$$

$$941 - 2\cdot 9 = 923,$$

$$92 - 2\cdot 3 = 86.$$

86 is not divisible by 7, so 9423242 is not divisible by 7.


It is difficult to factor a large, arbitrary integer in a reasonable amount of time. You can use simple divisibility tests like those above to deal with "obvious" cases, but the general problem is the object of current research. Here's a useful idea which is called Fermat factorization.

Proposition. Let n be an odd integer. There is a one-to-one correspondence

$$\left\{\matrix{\hbox{factorizations} \cr \hbox{of n} \cr}\right\} \leftrightarrow \left\{\matrix{\hbox{expressions of n as the} \cr \hbox{difference of two squares} \cr}\right\}$$

Proof. If $n
   = ab$ , n is odd so a and b are odd. Then $a + b$ and $a - b$ are even, so $\dfrac{a + b}{2}$ and $\dfrac{a - b}{2}$ are integers. Now

$$n = \left(\dfrac{a + b}{2}\right)^2 - \left(\dfrac{a - b}{2}\right)^2$$

expresses n as a difference of two squares.

Conversely, suppose n is written as as difference of squares: $n = s^2 - t^2$ . Then

$$n = (s - t)(s + t)$$

is a factorization of n.

You can check that these two procedures --- factors to difference and difference to factors --- "undo" one another.


Example. Use Fermat factorization to factor 4819.

The idea is to try to write 4819 as $s^2 - t^2$ . I will form $s^2 - 4819$ and increase s till I get a perfect square. What s's do I need to use?

First, $\sqrt{4819} \approx 69.4$ . Since $4819 = s^2 - t^2$ , s must be at least as big as $69.4 \approx
   70$ .

On the other hand, the factorization with the biggest factor is $4819 = 1\cdot 4819$ . By the proof of the last result, this would produce an s of the form $\dfrac{4819 + 1}{2}
   = 2410$ . So I need to try s for $70 \le s \le 2410$ .

On the very first try,

$$70^2 - 4819 = 4900 - 4819 = 81 = 9^2.$$

Thus, $s = 70$ and $t = 9$ . $s + t =
   79$ , $s - t = 61$ , and $79\cdot 61 = 4819$ .


Example. Use Fermat factorization to factor 779.

$\sqrt{779} \approx 27.91057$ , so I need $s \ge 28$ . $\dfrac{779 + 1}{2}
   = 390$ , so $s \le 390$ .

$$\vbox{\offinterlineskip \halign{& \vrule # & \strut \hfil \quad # \quad \hfil \cr \noalign{\hrule} height2pt & \omit & & \omit & \cr & s & & $s^2 - 779$ & \cr height2pt & \omit & & \omit & \cr \noalign{\hrule} height2pt & \omit & & \omit & \cr & 28 & & $28^2 - 779 = 5$ & \cr height2pt & \omit & & \omit & \cr \noalign{\hrule} height2pt & \omit & & \omit & \cr & 29 & & $29^2 - 779 = 62$ & \cr height2pt & \omit & & \omit & \cr \noalign{\hrule} height2pt & \omit & & \omit & \cr & 30 & & $30^2 - 779 = 121 = 11^2$ & \cr height2pt & \omit & & \omit & \cr \noalign{\hrule} }} $$

The factors are $30 + 11 = 41$ and $30 - 11 = 19$ : $779 = 41\cdot 19$ .


The Fermat numbers are numbers of the form

$$F_n = 2^{2^n} + 1.$$

Fermat thought that all the $F_n$ were prime. However, it turns out that $641 \mid F_5 = 2^{32} + 1$ . Note that

$$641 = 2^4 + 5^4 \quad\hbox{and}\quad 641 = 2^7\cdot 5 + 1.$$

Therefore,

$$2^7\cdot 5 = 641 - 1, \hbox{ and so } 2^{28}\cdot 5^4 = (641 - 1)^4 = 641\cdot \hbox{junk} + 1.$$

On the other hand, $5^4 = 641 - 2^4$ , so

$$2^{28}\cdot (641 - 2^4) = 641\cdot \hbox{junk} + 1,$$

$$641\cdot 2^{28} - 2^{32} = 641\cdot \hbox{junk} + 1,$$

$$2^{32} + 1 = 641\left(2^{28} - \hbox{junk}\right).$$

This proves that $641 \mid 2^{32} +
   1$ .

Here are some properties of the Fermat numbers.

Proposition. If p is prime and $p \mid F_n$ , then $p = k\cdot
   2^{n+2} + 1$ for some k.

I won't prove this result, since the proof requires some stuff about quadratic residues which I won't discuss for a while. Here's how it can be used.


Example. $F_4 = 2^{2^4} + 1 = 65537$ . Here $n = 4$ , so all prime divisors must have the form $k\cdot 2^6 + 1 = 64k + 1$ . There are around 1024 numbers less than 65537 of this form, but I only need to check numbers up to the square root $\sqrt{65537} \approx 256$ . (For if a number has a prime factor, it must have a prime factor less than its square root.)

$$\vbox{\offinterlineskip \halign{& \vrule # & \strut \hfil \quad # \quad \hfil \cr \noalign{\hrule} height2pt & \omit & & \omit & & \omit & \cr & k & & $64k + 1$ & & Conclusion & \cr height2pt & \omit & & \omit & & \omit & \cr \noalign{\hrule} height2pt & \omit & & \omit & & \omit & \cr & 1 & & 65 & & Not prime & \cr height2pt & \omit & & \omit & & \omit & \cr \noalign{\hrule} height2pt & \omit & & \omit & & \omit & \cr & 2 & & 129 & & Not prime & \cr height2pt & \omit & & \omit & & \omit & \cr \noalign{\hrule} height2pt & \omit & & \omit & & \omit & \cr & 3 & & 193 & & Prime, but doesn't divide 65537 & \cr height2pt & \omit & & \omit & & \omit & \cr \noalign{\hrule} }} $$

(The next value of $64k + 1$ is 257, which is larger than $\sqrt{65537}$ .) Conclusion: 65537 must be prime!


Proposition. $F_0F_1\cdots F_{n-1} = F_n - 2$ for $n > 0$ .

Proof. $F_0
   = 3$ and $F_1 = 5$ , so $F_0 = F_1 - 2$ . The result is true for $n = 1$ .

Take $n > 0$ , and assume the result is true for n; I'll try to prove it for $n + 1$ . By assumption,

$$F_0F_1\cdots F_{n-1} = F_n - 2, \quad\hbox{so}\quad F_0F_1\cdots F_{n-1} F_n = (F_n - 2)F_n.$$

Now

$$(F_n - 2)F_n = (2^{2^n} - 1)(2^{2^n} + 1) = 2^{2\cdot 2^n} - 1 = 2^{2^{n+1}} - 1 = 2^{2^{n+1}} + 1 - 2 = F_{n+1} - 2.$$

That is,

$$F_0F_1\cdots F_{n-1} F_n = F_{n+1} - 2.$$

This is the statement for $n + 1$ , so the proof is complete, by induction.

Proposition. If $m \ne n$ , $(F_m,F_n) = 1$ .

Proof. Assume $m < n$ (if not, switch m and n). Suppose p is prime and $p
   \mid F_m$ and $p \mid F_n$ . Also, $p \mid
   F_0F_1\cdots F_{n-1}$ since $m < n$ implies that $F_m$ occurs in this product. But

$$F_n - 2 = F_0F_1\cdots F_{n-1}, \quad\hbox{so}\quad 2 = F_n - F_0F_1\cdots F_{n-1}.$$

Since p divides both of the terms on the right, $p \mid 2$ , so $p = 2$ . This is impossible, since all the $F_n$ 's are odd.

Therefore, there is no prime dividing both $F_m$ and $F_n$ , and hence $(F_m,F_n) = 1$ .


Contact information

Bruce Ikenaga's Home Page

Copyright 2008 by Bruce Ikenaga