Richard Guy’s Strong Law of Small Numbers says

There aren’t enough small numbers to meet the many demands made of them.

In his article by the same name [1] Guy illustrates his law with several examples of patterns that hold for small numbers but eventually fail. One of these examples is

3! – 2! + 1! = 5

4! – 3! + 2! – 1! = 19

5! – 4! + 3! – 2! + 1! = 101

6! – 5! + 4! – 3! + 2! – 1! = 619

7! – 6! + 5! – 4! + 3! – 2! + 1! = 4421

8! – 7! + 6! – 5! + 4! – 3! + 2! – 1! = 35899

If we let *f*(*n*) be the alternating factorial sum starting with *n*, *f*(*n*) is prime for *n* = 3, 4, 5, 6, 7, 8, but not for *n* = 9. So the alternating sums aren’t all prime. Is *f*(*n*) usually prime? *f*(10) is, so maybe 9 is the odd one. Let’s write a code to find out.

from sympy import factorial, isprime
def altfact(n):
sign = 1
sum = 0
while n > 0:
sum += sign*factorial(n)
sign *= -1
n -= 1
return sum
numprimes = 0
for i in range(3, 1000):
if isprime( altfact(i) ):
print(i)
numprimes += 1
print(numprimes)

You could speed up this code by noticing that

altfact(n+1) = factorial(n+1) - altfact(n)

and tabulating the values of `altfact`

. The code above corresponds directly to the math, though it takes a little while to run.

So it turns out the alternating factorial sum is only prime for 15 values less than 1000. In addition to the values of *n* mentioned above, the other values are 15, 19, 41, 59, 61, 105, 160, and 601.

* * *

[1] The Strong Law of Small Numbers, Richard K. Guy, The American Mathematical Monthly, Vol. 95, No. 8 (Oct., 1988), pp. 697-712.

For daily tips on Python and scientific computing, follow @SciPyTip on Twitter.