/ home / blog
brain activity log

02.01.2007 - Tuesday - 02:55 - Primality Tests

Happy New Year to everyone :)

At a certain point, in the middle of the New Year party we ended up talking about prime numbers. The simple question was if 2007 was prime or not. A guy immediately comed out with the answer: no, it's divisible by 3. He used a simple "rule of thumb": if the digits of a number sum up to something that is divisible by 3 then the original number is also divisible by 3. And in fact 2+0+0+7 yelds 9 that is obviously 3*3.

So 2007 is not a prime year. But how is it that the rule of thumb works ? We were quite convinced that the rule is correct by carrying out a ton of working examples but (probably because of alcohol :D) were unable to prove it.

In fact a pedantic proof is not as trivial as one might expect for such a simple rule. Anyway, a bit of thinking (with a sane mind) pulled it out. Here it is.

Theorem

Given an integer x let y be the sum of its decimal digits. If y is divisible by 3 then x is also divisible by 3 and vice-versa.

Since for x=0 the theorem is trivially true, let x be a non-zero integer and consider it's positional rappresentation:

The digits di in the sum are obviously the positional digits of x ordered by magnitude (power of ten). Let y be the sum of the digits of x.

Asserting that "if y is divisible by 3 then x is also divisible by 3 and vice-versa" is equivalent to asserting that "if there exists an integer w that yelds x=3w then there must exist an integer v that yelds y=3v and vice-versa".

A yet more convenient way to formally state the theorem thesis is: given

prove that "if w lies in Z then also v lies in Z and vice-versa".

To prove this let's concentrate on the positional rappresentation of our number.

We can trivially rewrite it as

which can be decomposed in

which clearly shows that the second component is the sum of the digits defined above and allows us to write

and then rewrite it further as

Now if we assume that w is an integer then v is an integer (lies in Z) if and only if

The same condition remains if we first assume v to be an integer and ask for w being an integer: it still leads us to prove that

Now if you're cool, you should have already be "seeing" the end of this proof since the formula above can be "trusted" to be true if you realize that 10^i - 1 leads always to a number with digits formed by a sequence of all 9.... but let's be pedantic and prove this formally.

To do this we need to take a quick look at a nice property of the foolowing geometric serie:

We can multiply both sides by (a-1) to obtain

Expliciting the multiplication

and removing the cancelling terms we get

which is quite interesting since by plugging in a=10 it allows us to write that

and for i=k+1 -> k=i-1 it allows to rewrite our clue formula as

and then by trivial algebric manipulation

Remember that in the formulas above we have assumed x != 0 (since, as stated at the beginning, for x=0 the theorem is trivially true). This in turn requires i > 0 and thus i-1 is at least 0.

Finally since , since 10^j is an integer and a sum of integers is still an integer it's obvious that

which proves our theorem.

Cool eh? What a strange world... :)

It is interesting to state that this thingie can be applied recursively. If y is too big to be trivially known to be divisible by 3 then the sum of its digits can be taken and the theorem can be applied again.

Another note is that the theorem will work for bases B that can be written in the form B=3N+1 where N is an integer. It will work for base 4, 7, 10, 13, 16... but not for base 2 (for example). This can be proven by substituting (3N+1) in the whole theorem in place of 10 and noticing that it will lead us to prove that

The geometric serie property is still valid and yelds

which is rewritten as

This allows us to rewrite our question as

which, since di and N are integers, yelds the obvious conclusion that

Marvellous...

One can push this thingie even further and assert that to verify the divisibility by a number D one has to write the number x in a base B that can be written as B=(DN+1), where N is an integer greater than zero, sum up the digits of B and verify if the obtained number is divisible by D.

This can be proven by reusing the last version of the theorem and plugging D instead of 3. The question will now be

And it will lead us to state that

Which is indeed true since D, N and di are integers.

Interesting. So if you want to know if 3147975 is divisible by 255 all you could write 3147975 in a base of the form 255N+1, for example 256 (huh.. we're talking of BYTES!), sum up the digits and check if it's divisible by 255, recursively.

3147975 is (48)(8)(199) in base 256 which magically sums up to 255.... doh!

I could have written this thingie in base 255*2+1=511, which would yeld a rappresentation of (12)(28)(215) which again sums up to 255.

Unbelieveable...

The bad news, eventually, is that this theorem can't be easily used as a general primality test since it involves base changes. A generic change from base A to base B requires consecutive divisions of the original number... so one can actually directly divide the number by the divisor and check if there is a remainder... This can be used as primality test for some divisors, in particular the ones that lead to the bases B to which switching is easy. Switching from base 10 to base 100 is straightfoward and does not require any division to be performed. Same goes for base 2 and base 16, for example.

Now I have to work. Further investigations later.

Have a nice day!



want more ?

... really ? :D

Browse around then.

You're viewing one post per page: you can view more, if you want.

The entries marked in red are the ones you're viewing now.

2007.09.18-06.07
2007.09.09-04.27
2007.08.26-02.23
2007.08.21-02.02
2007.08.12-20.15
2007.07.22-11.30
2007.07.21-10.00
2007.07.11-17.20
2007.06.10-02.57
2007.06.02-03.33
2007.05.30-20.07
2007.05.21-11.35
2007.04.14-21.53
2007.03.26-02.22
2007.03.23-10.12
2007.03.20-18.10
2007.03.16-01.10
2007.03.12-21.43
2007.03.12-03.57
2007.03.05-13.08
2007.02.25-22.21
2007.02.14-23.30
2007.01.02-02.55
2006.12.17-17.01
2006.11.26-20.26
2006.11.22-03.14
2006.11.21-01.30
2006.11.04-05.09
2006.09.16-04.18
2006.08.18-03.45
2006.08.14-17.58
2006.08.14-03.08
2006.08.02-20.38
2006.07.25-04.10
2006.07.25-03.14
2006.06.23-18.12
2006.06.02-13.25
2006.05.18-16.27
2006.05.18-14.30
2006.05.17-19.30
2006.04.29-19.30
2006.04.26-01.48
2006.04.22-13.06
2006.04.16-12.26
2006.04.11-03.10
2006.04.10-04.32
2006.04.08-14.59
2006.04.07-14.54
2006.04.06-09.00
2006.04.05-23.10
2006.04.05-11.00
2006.04.04
2006.04.03
2006.04.02
2006.04.01
2006.03.31
2006.03.27
2006.03.26
2006.03.25
2006.03.24
2006.03.23
2006.03.22
2006.03.20
2006.03.19
2006.03.17
2006.03.14
2006.03.07
2006.03.05
2006.02.23
2006.02.19
2006.02.13
2006.01.10
2005.12.29
2005.09.24
2005.09.21
2005.08.21
2005.08.18
2005.07.31
2005.07.04
2005.06.13
2005.04.10
2005.04.05
2004.12.18
2004.12.17
2004.12.16
2004.12.15
2004.12.14
2004.12.13
2004.12.12
2004.12.11
2004.12.10
2004.12.09
2001.06.02