Problem with exponent of p in n! algorithm (was: a(27) new higher lower bound)

Alonso Del Arte alonso.delarte at gmail.com
Tue Nov 22 23:26:33 CET 2005


David, there is a problem with the algorithm

   int e(int p, int n) {
       int s = 0;
       while (n > 0) {
           n /= p;   // That is, n = floor(n/p)
           s += n;
       }
       return s;
   }

I implemented in Mathematica thus:

expoPF[p_, n_] := Module[{s, x}, x =
   n; s = 0; While[x > 0; x = Floor[x/p]]; s = s + x; s]

then gave the command

Table[expoPF[2, n], {n, 25}]

which gave the results {0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
8, 8, 9, 9, 10, 10, 11, 11, 12, 12} (A004526, Integers repeated.)

Instead I expected {0, 1, 1, 3, 3, 4, 4, 7, 7, 8, 8, 10, 10, 11, 11,
15, 15, 16, 16, 18, 18, 19, 19, 22, 22} (A011371, n minus (number of
1's in binary expansion of n). Also highest power of 2 dividing n!.).

Alonso






More information about the SeqFan mailing list