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.

A different question one could ask and is interesting is whether there are infinitely many alternating factorials which are prime.

Again I wanted to try this in Perl 6.

I noticed that each factorial was only used once, so I figured I would use the iterator API so that it would use a minimal amount of RAM.

my \factorial = ( 1, { ++$ * $_ } … * ).iterator;

my \alt-factorial = ( { factorial.pull-one – ($_//1)} … * ).iterator;

my @alt-primes = (^1000).grep: { alt-factorial.pull-one.is-prime }

say @alt-primes; # (3 4 5 6 7 8 10 15 19 41 59 61 105 160 661)

say +@alt-primes, ‘ primes’; # 15 primes

say now – INIT { now }, ” seconds”; # 127.4444326 seconds

Wondering the same question as Kosta, it occurred to me that if any f(n) has a factor <= n+1, the all f(n+k) would also have that as a factor. So the number of prime f(n) would be finite. Some C++ code and an hour of computation revealed n==3612702 as meeting that criteria.

Only then did I check Wikipedia (https://en.wikipedia.org/wiki/Alternating_factorial) and see this fact as part of the article.

BTW, you list n=601 as a prime f(n), but others give n=661 instead.