The number of digits in n! (Part 3)

The following graph shows the number of digits in n! as a function of n.

factorialdigits

When I was in school, I stared at this graph for weeks, if not months, trying to figure out an equation that would fit these points. And I never could figure it out.

When I took calculus in college, I distinctly remember getting up the nerve to ask my professor, the great L.Craig Evans (now at UC Berkeley), if he knew how to solve this problem. To my great consternation, he immediately wrote down what I now realize to be the right answer, using Stirling’s approximation:

\ln n! \approx \left(n + \displaystyle \frac{1}{2} \right) \ln n - n + \frac{1}{2} \ln (2\pi)

While I now know that this was the way to go about solving this problem, I didn’t appreciate how this formula could help me at the time. I only saw the n! on the left-hand side and did not see the immediate connection between this formula and the number of digits in n!.

But now I know better.

For starters, the number of base-10 digits in a number n is always the next integer greater that \log_{10} n. For example, $\log_{10} 2000 \approx 3.301$, and the next integer larger than 3.301 is 4. Unsurprisingly, the number 2000 has 4 digits.

Second, the change of base formula for logarithms gives

\log_{10} n! = \displaystyle \frac{\ln n!}{\ln 10}

Therefore, the number of digits in n! will be about

\displaystyle \frac{\left(n + \displaystyle \frac{1}{2} \right) \ln n - n + \frac{1}{2} \ln (2\pi)}{\ln 10}

The graph below shows just how accurate this approximation really is. The solid curve is the approximation; the dots are the values of \log_{10} n!. (In other words, this series of dots are only slightly different than the dots above, which have integers as coordinates.) Not bad at all… the error in the approximation is smaller than the size of the dots in this picture.

stirling

The number of digits in n! (Part 2)

The following graph shows the number of digits in n! as a function of n.

factorialdigits

When I was in school, I stared at this graph for weeks, if not months, trying to figure out an equation that would fit these points. And I never could figure it out.

In retrospect, my biggest mistake was thinking that the formula had to be something like y = a x^m, where the exponent m was a little larger than 1. After all, the graph is clearly not a straight line, but it’s also not as curved as a parabola.

What I didn’t know then, but know now, is that there’s a really easy way to determine to determine if a data set exhibits power-law behavior. If y = a x^m, then

\ln y = \ln a + m \ln x.

If we make the substitutions Y = \ln Y, B = \ln a, and X = \ln x, then this equation becomes

Y = m X + B

In other words, if the data exhibits power-law behavior, then the log-transformed data would look very much like a straight line. Well, here’s the graph of (X,Y) after applying the transformation:

loglogfactorialdigits

Ignoring the first couple of pots, the dots show an ever-so-slight concave down pattern, but not enough that would have discouraged me from blindly trying a pattern like y = a x^m. However, because these points do not lie on a straight line and exhibit heteroscedastic behavior, my adolescent self was doomed to failure.

The number of digits in n! (Part 1)

When I was in school, perhaps my favorite pet project was trying to find a formula for the number of digits in n!. For starters:

  • 0! = 1: 1 digit
  • 1! = 1: 1 digit
  • 2! = 2: 1 digit
  • 3! = 6: 1 digit
  • 4! = 24: 2 digits
  • 5! = 120: 3 digits
  • 6! =720: 3 digits
  • 7! = 5040: 4 digits
  • 8! = 40,320: 5 digits

I owned what was then a top-of-the-line scientific calculator (with approximately the same computational capability as a modern TI-30), and I distinctly remember making a graph like the following on graph paper. The above calculations contribute the points (0,1), (1,1), (2,1), (3,1), (4,2), (5,3), (6,3), (7,4), and (8,5).

factorialdigitsI had to stop (or, more accurately, I thought I had to stop) at 69! because my calculator couldn’t handle numbers larger than 10^{100}.

I stared at this graph for weeks, if not months, trying to figure out an equation that would fit these points. And I never could figure it out.

And, to this day, I’m somewhat annoyed at my adolescent self that I wasn’t able to figure out this puzzle for myself… since I had all the tools in my possession needed to solve the puzzle, though I didn’t know how to use the tools.

In this series of posts, I’ll answer this question with the clever application of some concepts from calculus and precalculus.

Too many significant digits (Part 2)

In yesterday’s post, I had a little fun with this claim that the Nike app could measure distance to the nearest trillionth of a mile. The more likely scenario is that the app just reported all of the digits of a double-precision floating point number, whether or not they were significant.

significantdigits

In real life, I’d expect that the first three decimal places are accurate, at most. According to Wikipedia, the official length of a marathon is 42.195 kilometers, but any particular marathon may be off by as many as 42 meters (0.1% of the total distance) to account for slight measurement errors when figuring out a course of that length.

A history about the Jones-Oerth Counter, the devise used to measure the distance of road running courses, can be found at the USA Track and Field website. And my friends who are serious runner swear that the Jones-Oerth Counter is much more accurate than GPS.

green line
The same story often appears in students’ homework. For example:

If a living room is 17 feet long and 14 feet wide, how long is the diagonal distance across the room?

Using the Pythagorean theorem, students will find that the answer is \sqrt{485} feet. Then they’ll plug into a calculator and write down the answer on their homework: 22.02271555 feet.

This answer, of course, is ridiculous because a standard ruler cannot possibly measure a distance that precisely. The answer follows from the false premise that the numbers 17 and 14 are somehow exact, without absolutely no measurement error. My guess is that at most two decimal places are significant (i.e., the numbers 17 and 14 can be measured accurately to within one-hundredth of a foot, or about one-eighth of an inch).

My experience is that no many students are comfortable with the concept of significant digits (or significant figures), even though this is a standard topic in introductory courses in chemistry and physics. An excellent write-up of the issues can be found here: http://www.angelfire.com/oh/cmulliss/

Other resources:

http://mathworld.wolfram.com/SignificantDigits.html

http://en.wikipedia.org/wiki/Significant_figures

http://en.wikipedia.org/wiki/Significance_arithmetic

 

Too many significant digits (Part 1)

The following appeared on my Facebook feed a while back:

significantdigits

Just look at that: the Nike app claimed to measure the length of my friend’s run with twelve decimal places of accuracy.

Let’s have some fun with this. Just suppose that the app was able to measure distance to the nearest trillionth of a mile. One trillionth of a mile is…

5.28 billionths of a foot,

or about 63.4 billionths of an inch,

or about 161 billionths of a centimeter,

or about 1.61 billionths of a meter,

or about 1.61 nanometers.

By way of comparison, the fingernails on the average adult grow about 3 millimeters a month. A world-class runner could run 6.25 miles in about 30 minutes; in those 30 minutes, his/her fingernails would grow about 2 microns, or about 2000 nanometers. (Of course, they’ll grow longer for less athletic runners covering the same distance at a slower speed.)

So if the Nike app can measure my distance to the nearest trillionth of a mile, it would have absolutely no difficulty measuring how much my fingernails grew while running.

Or, it could be that the Nike app really isn’t measuring the distance all that precisely. Probably the app used double-precision arithmetic, and whoever programmed the app didn’t tell it to truncate after a reasonable number of digits.

Lychrel Numbers

A friend of mine posted the following on Facebook (with names redacted):

So [my daughter] comes home with this assignment:

For each number from 10 – 99, carry out the following process.

  1.  If the number is a palindrome (e.g., 77), stop.
  2.  Else reverse the number and add that to the original. E.g.: 45+54 = 99.
  3.  If the result is not a palindrome, repeat step (2) with the result.
  4.  Record the final palindromic result and the number of steps taken.

Most are simple.

  • 56 + 65 = 110
  • 110 + 011 = 121
  • Stop. 2 steps taken.

The numbers 89 and 98 were given for extra credit, and they mysteriously explode, taking 24 steps. It made [my daughter] cry.

She wanted me to check her work, so I decided it was a good time to teach the wonders of Python, and we very quickly had a couple of simple functions to do the trick.

Well, you saw where this was going. How many steps does 887 take?

We’re up to 104000 steps so far, and Python is crying.

True or false: For a given n, the above algorithm completes in finite time?

I guess I’ve been living under a rock for the past 20 years, because I had never heard of this problem before. It turns out that numbers not known to lead to a palindrome are called Lychrel numbers. However, no number in base-10 has been proven to be a Lychrel number. The first few candidate Lychrel numbers (i.e., numbers that have not been proven to not be Lychrel numbers) are 196, 295, 394, 493, 592, 689, 691, 788, 790, 879, 887, 978, 986, 1495, 1497, 1585, 1587, 1675, 1677, 1765, 1767, 1855, 1857, 1945, 1947, 1997, 2494, 2496, 2584, 2586, 2674, 2676, 2764, 2766, 2854, 2856, 2944, 2946, 2996, 3493, 3495, 3583, 3585, 3673, 3675…

The above algorithm is called the 196-algorithm, after the smallest suspected Lychrel number.

For further reading, I suggest the following links and the references therein:

http://mathworld.wolfram.com/196-Algorithm.html

http://mathworld.wolfram.com/LychrelNumber.html

http://www.p196.org/

http://www.mathpages.com/home/kmath004/kmath004.htm (which contains a proof that 10110 is a Lychrel number in binary and that Lychrel numbers always exist in base 2^k)

http://en.wikipedia.org/wiki/Lychrel_number

Proof without words: The difference of consecutive cubes

Source: https://www.facebook.com/photo.php?fbid=451328078334029&set=a.416585131808324.1073741827.416199381846899&type=1&theater

For a more conventional algebraic proof, notice that

(n+1)^3 - n^3 = n^3 + 3n^2 + 3n + 1 - n^3 = 3n(n+1) + 1

The product n(n+1) is always an even number times an odd number: if n is even, then n+1 is odd, but if n is odd, then n(n+1). So n(n+1) is a multiple of 2, and so 3n(n+1) is a multiple of 6. Therefore, 3n(n+1)+1 is one more than a multiple of 6, proving the theorem.