There are two numbers we can change in the de…nition of f (m): the sum of the squares of the digits of m. One is the number 2 that we use because we are summing the squares of the digits. We could just as well sum the cubes of the digits, or the fourth powers of the digits. Call that number e, for exponent. For the original f , we have e = 2. It’s easy to change the recursion for f (m) so that it now computes the sum of the cubes of the digits of m.
How do you do that?
The other number we can change is the number 10. That is the base b for our system of representing numbers by strings of digits. If we change the number b = 10 to, say, b = 8, then the digits we get are the digits in the base-8 representation of m, that is, we are thinking of writing m in octal.
The octal representation of the number 340779 is “1231453”, or 12314538 if we want to indicate that the number is written in base 8. The sum of the squares of its digits is
1 + 4 + 9 + 1 + 16 + 25 + 9 = 65 = 1018
and if we continue summing the squares of the digits we get 2, 4, 16 = 208, 4 and we have run into a periodic point, 4, with period 2.
Let’s examine in some detail what happens when we repeatedly sum the squares of the digits of a number in base 5. Here is the beginning of a table:
1 ! 1
2 ! 4 ! 16 = 315 ! 10 = 205 ! 4
Let f (n) denote the sum of the squares of the digits of n when we write n in base 5. Then f (1) = 1 because 1 = 15. The number 2 is equal to 25 so f (2) = 4. The number 4 is equal to 45, so f (4) = 16 = 315. Now f (315) = 10 = 205 and f (205) = 4, so we are back at 4, and we have constructed the orbit 4 16 10. The next entry in our table is
3! 9 = 145 ! 17 = 325 ! 13 = 235 ! 13
so we have discovered another …xed point: f (13) = 13, an orbit of size 1.
Continue this table up to n = 18. How many orbits do you …nd?
You can easily change your program for computing f (m) to one that computes f (m; e; b), the sum of the e-th powers of the digits of m, where m is written in base b. Your …rst function f (m) was f (m; 2; 10).
Exercise 1. Find the smallest number n > 1 such that n is happy in all bases 2 through 8.
Exercise 2. Show that there is no number greater than 1 that is happy in all bases.
Exercise 3. Show that every number is happy in bases 2 and 4. These numbers are called “happy bases”. It is claimed (http://oeis.org/A161872) that 2 and 4 are the only happy bases less than 500; 000; 000.
Exercise 4. It is claimed that the greatest happy number (base 10) that has no repeated digits is 986543210. Verify that claim.
Exercise 5. Find all the orbits of the functions f (m; 2; 7) and f (m; 2; 13).
8 Finding orbits
We are interested in the function f (m; e; b) that takes a number m to the sum of the e-th powers its digits when it is written in base b. We examined f (m; 2; b) in the previous section. The function f (m; e; 10) maps a positive
integer m to a positive integers. Such a function f gives us a discrete dynami-cal system. We are particularly interested in the …xed points of such a system:
numbers m such that f (m) = m. The …xed points m of f (m; 3; 10) are 1, 153, 370, 371, and 407. We are also interested in periodic points which come in groups called orbits. An orbit is a …nite set S of distinct positive integers fs1; s2; : : : ; skg so that f (si) = si+1, for i < k, and f (sk) = s1. If k = 1, then the orbit S contains exactly one number, and that number is a …xed point of f . An orbit of size 3 of the function f (m; e; 10) is f55; 250; 133g, as you can easily check by hand.
It turns out that if we choose any positive integer m whatsoever, and keep applying f (m; e; b) to it, then eventually we end up in an orbit. To verify that claim, we proceed as we did for the unhappy numbers: show that f (m) < m for all su¢ ciently large values of m. If m is has k-digits when written in base b, then f (m; e; b) is at most k (b 1)e which happens when all k of the digits of m are equal to b 1, the largest digit. How many digits can the number k (b 1)e have when written in base b? If a number is less than bd, then it has at most d digits, so we would like know how large k must be to guarantee that k (b 1)e < bk 1. However, for theoretical purposes, it is enough to know that bk 1=k gets arbitrarly large for large values of k.
That’s because all we need is for bk 1=k eventually to be bigger than the
…xed number (b 1)e. Here is a graph of the function y = 2x 1=x
2 4 6 8
0 1 2 3 4 5 6 7 8 9 10
x y
Looks like it is going o¤ to in…nity! We can use l’Hôpital’s rule to show that.
Taking the derivative of the top and the bottom of 2x 1=x we get 2x 1ln 2
which clearly goes to in…nity.
How can we …nd orbits of f ? The simplest thing to do is to choose some number n, either systematically or at random, and form the sequence n, f (n) ; f (f (n)) ; f (f (f (n))) ; : : :. There are really only two things that can happen, one of which we can detect. Either the sequence never repeats, or at some point we will generate a number that we have seen before. For all of the functions f (m; e; b), no matter what e and b are, this sequence will eventually repeat. How do we …gure out where that happens?
Given a function f , we want to write a program that …nds the …rst place k such that fk(m) = fi(m) for some i < k. More precisely, we want to write a program that returns fk(m), where k is the …rst place such that fk(m) = fi(m) for some i < k. For the function f (m; 2; 10), if we start at m = 11, and repeatedly apply f , we get the sequence
11; 2; 4; 16; 37; 58; 89; 145; 42; 20; 4
so we want to return 4. To do that, we have to keep track of where we have been. We can do that using a list or using a set. We’ll do it with a set S here. At the start, we haven’t been anywhere, so S is empty. To set S equal to the empty set, we write S = set(). The standard way to generate the sequence that we want is to repeatedly set n equal to f (n). Of course we don’t want to do that forever, so we want to stop when we’ve seen n before, that is, when n is in S. The easiest way to do that is to write a while-loop:
while not n in S:. We also want to update S each time we look at another value of n by putting n in S. That’s done by writing S.add(n). Those are all the ingredients— write the function.
If we use a list instead of a set, we can easily get our function to return the orbit that n eventually ends up in. So now we have a list L that keeps track of where we have been. Start out by setting L = [], and write L = L + [n] instead of S.add(n). Instead of returning n when we leave the while-loop, return the slice L[ L.index(n) : ]. The function L.index(n) returns the position of the number n in the list L. The list L[ a : b ] is the slice of L that goes from position a up to, but not including, position b. The list L[ a : ] is the slice of L that goes from position a to the end of L. So L[ L.index(n) : ] is the
slice of the list L starting at the number n and going to the end of L. This is exactly the orbit that we end up in because n, at this point, is the …rst number we have seen twice on our journey.
9 Aliquot sequences
One very much studied discrete dynamical system is given by the function s (n) which returns the sum of the proper divisors of n. Here we include the divisor 1, even though it is rather uninteresting, but we don’t include n itself.
So s (2) = 1, s (3) = 1, s (4) = 1 + 2 = 3, s (5) = 1, and s (6) = 1 + 2 + 3 = 6.
Notice n is prime exactly when s (n) = 1. The number 6 is called perfect because s (6) = 6. It is a …xed point s.
Technically, s (1) = 0 because 1 doesn’t have any proper divisors. I guess we could say that s (n) is the sum of the positive divisors of n that are less than n.
The study of perfect numbers goes back to antiquity. A related concept is that of a pair of amicable numbers. These are distinct numbers m and n such that s (m) = n and s (n) = m. That is, the pair fm; ng is an orbit of size 2. Actually, we say that it is an orbit of period 2. The smallest amicable pair is f220; 284g. Bigger orbits are called sets of sociable numbers. There is no orbit of period 3. An example of an orbit of period 4 is f1264460; 1547860; 1727636; 1305184g.
Unlike the other functions we have looked at, there is no guarantee that if we keep applying the function s, we will eventually reach a …nite orbit. In fact, this is a major unsolved problem. The …rst problematic number is 276.
10 The Collatz function
Another much studied dynamical system is given by the function f (n) = n=2 if n is even
3n + 1 if n is odd
If you start at 1 you get the sequence 1; 4; 2; 1, an orbit of size 3. If you start at 3 you get the sequence 3; 10; 5; 16; 8; 4 ending up in the same orbit of size 3. The sequence starting at 27 is quite interesting, but also ends up in that same orbit. The Collatz conjecture is that no matter where you start,
you always end up in this orbit. So the conjecture is that there are no other orbits, and no sequence can wander o¤ to in…nity.
It’s unlikely that we will be able to …nd any other orbits, but we can ask the question: if you start at a number n, what is the biggest number you will see if you keep applying the function f ? If you start at the number 1, the biggest number you see is 4. If you start at the number 2, the biggest number you see is 4— nothing new. If you start at the number 3, the biggest number you see is 16. A small table is
start 1 2 3 4 5 6 7 8 9
biggest number encountered 4 4 16 16 16 16 52 4 52 The interesting numbers here are the ones that set new records; those are the numbers 1, 3, and 7 which set the new records 4, 16, and 52. (Try calculating by hand the largest number you get when you start with 7.) The next number to set a new record is 27 from which you eventually get to the record high of 9232. So we get a smaller table
start 1 3 7 27
new record 4 16 52 9232
Each number in the …rst row reaches a value greater than that reached by any previous number (a new record). The second row shows what those values are.
Project: Extend this table to include, in addition to the numbers, 1; 3; 7; 27, all record holders less than 10000. You probably don’t want to do this by hand— write a Python program instead.
11 Bulgarian solitaire
Bulgarian solitaire, a game popularized by Martin Gardner, is played with stacks of a …xed number of coins. For example, suppose you start with 10 coins divided into two stacks of 4 coins each and one stack of 2 coins. We denote this con…guration by 442. A move in the game consists of taking one coin from each stack to form a new stack. That’s it! What happens if you start with 442? You take one coin from each stack, leaving two stacks of 3 coins each and one stack of 2 coins, and you form a new stack with those 3 coins you skimmed o¤. We denote this move by 442 ! 3331. The game
continues 3331 ! 4222 ! 43111 ! 532 ! 4321 ! 4321 and we are stuck— a
…xed point.
If we start with a single stack of 9 chips, the game goes 9 ! 81 ! 72 ! 621 ! 531 ! 432 ! 3321 ! 4221 ! 4311 ! 432 and we are stuck in an orbit of size four.
See http://mancala.wikia.com/wiki/Bulgarian_Solitaire
Project: Write a program that plays this game. I represented a con…g-uration by a list L so that L [i] is the number of stacks of size i, but maybe there’s a better way to do it. Perhaps the con…guration 4311 should be rep-resented by the list [4; 3; 1; 1] rather than the list [0; 2; 0; 1; 1] which is how I did it.
Exercise 1. Show that the …xed points of Bulgarian solitaire are the con…gurations 1, 21, 321, 4321, 54321, etc. Note that there are two parts to this exercise: showing that those con…gurations are …xed points, and showing that any …xed point is one of those con…gurations.
Exercise 2. Find some other orbits of size greater than one. Can you guess what the general form is?
12 The digits of n-factorial
Here are the …rst few values of n-factorial
0! 1
1! 1
2! 2
3! 6
4! 24
5! 120
6! 720
7! 5040
8! 40 320 9! 362 880 10! 3628 800 11! 39 916 800
Our …rst project is to compute how many zeros appear at the end of n!. Our second project is to compute how many zeros as digits anywhere in n!, how many ones, how many twos, and so on, up to how many nines. In the number
11!, the digit 0 appears twice (both at the end), the digit 1 appears once, the digit 2 never appears, and so on. Here is the number 200!
788 657 867 364 790 503 552 363 213 932 185 062 295 135 977 687 173 263 294 742 533 244 359 449 963 403 342 920 304 284 011 984 623 904 177 212 138 919 638 830 257 642 790 242 637 105 061 926 624 952 829 931 113 462 857 270 763 317 237 396 988 943 922 445 621 451 664 240 254 033 291 864 131 227 428 294 853 277 524 242 407 573 903 240 321 257 405 579 568 660 226 031 904 170 324 062 351 700 858 796 178 922 222 789 623 703 897 374 720 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
Notice that there are a whole bunch of zeros at the end. I count forty-nine of them. That means that 200! is divisible by 1049 but not by 1050.
Exercise 1. Write a program that uses that fact to calculate how many zeros there are at the end of n!. Compute the number t = n!, then keep dividing t by 10 until you can’t anymore. If you keep track of how many times you divided by 10, you will know how many zeros n! ends in.
Another approach is to count how many factors of …ve there are in n!.
There is one zero at the end of n! for each factor of 5. That’s because n!
contains many more twos than …ves, so the number of factors of …ve will be equal to the number of factors of ten. Question: Why not use tens here?
Of the numbers 1; 2; 3; : : : ; n, the number that are divisible by 5 is equal to bn=5c the ‡oor of n=5, and each of them will contribute (at least) one 5 to the product of them all, n!. The number that are divisible by 52 is equal to bn=52c, and these will each contribute (at least) one additional 5 to the product. Continuing in this way we see that the number of 5’s in n! is equal
to jn
where we continue the series as long as n 5k, after which all the terms are zero. Notice that this sum is approximately
n 1 5 + 1
52 + 1
53 + = n
4
where we sum the in…nite geometric series to get 1=4. When n is 200, this estimate gives 200=4 = 50, pretty close to the actual number which is 49 as we have seen. The exact computation in this case is
200
5 + 200
25 + 200
125 = 40 + 8 + 1 = 49
In Python 2, we can realize n=5k by n/pow(5,k).
Exercise 2. Write the program!
A third approach in Python is to compute n! and then convert it into a string. We wouldn’t even consider this approach, except that that is the best way to do the next problem. Once we have a string s that holds the digits of n!, we can work our way from the right, starting at s [ 1], until we hit a nonzero digit. If that digit is at position m in the string, then the string has m 1zeros at the end.
Exercise 3. Write the program!
The web site
http://2000clicks.com/MathHelp/BasicFactorialDigitFrequency.aspx gives the digit frequencies of n! for n 500.
The zeros at the end cause factorials to have more zeros than expected.
Call the other zeros and digits, "inside zeros", and "inside digits". Is it true that the ratio of the inside zeros to the inside digits is approximately 1=10, and approaches this ratio in the limit as n gets large? Probably one of those impossible-to-answer questions, although we can calculate that ratio for all n less than, say, 100; 000.
Zero-perfect factorials: inside zeros appear with frequency exactly 1=10. Looks like the zero-perfect factorials with n < 2000 are 15!, 24!, 54!, 146!, 326!, 643!, 732!, 1097!, 1211!, 1386!. We can also ask what are the one-perfect factorials, the two-perfect factorials, and so on. The number 15!
is d-perfect for d = 0; 1; 4; 8. The numbers 146! and 732! are d-perfect for d = 0; 1, and possibly other digits. It’s not likely that we will encounter a number n so that n! is d-perfect for every digit d.
If s is a Python string, then s.count(i) will return the number of occur-rences of i in s. So if m = n! and s is str(m), then s.count(’0’) returns the number of occurrences of zero in n!, both inside and at the end.
12.1 Benford’s law
States that the probability that the …rst digit of a number is d is equal to log10(1 + 1=d) = log10(1 + d) log10(d). So the probability that the …rst digit is 1 is log10(2), about 30%. Test on n! and bn. See Julian Havil’s book, “Impossible?”. Here are the actual and predicted results for 2n where
n = 1; 2; : : : ; 2000
1 2 3 4 5 6 7 8 9
602 354 248 194 160 134 114 105 89 602 352 250 194 158 134 116 102 92 Here are the results for n!
1 2 3 4 5 6 7 8 9
591 335 250 204 161 156 107 102 94 602 352 250 194 158 134 116 102 92
13 Sequences of zeros in 2
nThe …rst power of two that contains the digit 0 (in decimal notation) is 210 = 1024. Is there a power of 2 that contains two consecutive 0’s? Well, 253= 9007 199 254 740 992 does, and it is the …rst one that does. How about three consecutive 0’s? Consider 2242 = 7067 388 259 113 537 318 333 190 002 971 674 063 309 935 587 502 475 832 486 424 805 170 479 104. Do you see the three consecutive zeros? Drawing on the work of Edgar Karst and U. Karst, Mathematics of Computation, July 1964, Julian Havil gives the following table of the …rst appearances of consecutive zeros in powers of 2:
2 3 4 5 6 7 8
53 242 377 1491 1492 6801 14007
Here is 2377 = 307 828 173 409 331 868 845 930 000 782 371 982 852 185 463 050 511 302 093 346 042 220 669 701 339 821 957 901 673 955 116 288 403 443 801 781 174 272 .
Finally, here is 21491 = 68 505 199 434 441 481 928 960 132 734 923 550 768 601 593 516 271 150 047 023 043 017 169 851 270 631 488 679 017 736 106 611 268 089 166 309 388 272 338 901 170 497 165 901 308 515 144 865 310 386 727 931 343 535 071 260 408 393 094 700 786 546 061 451 067 454 457 391 289 333 260 647 178 629 423 723 325 244 889 881 619 755 782 509 111 430 765 139 535 811 541 502 291 720 957 726 164 276 777 120 274 754 519 441 816 831 362 983 266 849 142 056 649 908 740 853 890 886 083 405 397 212 467 377 035 143 021 587 233 073 571 308 500 000 341 901 085 017 693 125 848 012 770 286 553 757 194 752 360 448. You can see where the …ve consecutive zeros are, and why 21492 contains six consecutive zeros.
There is a remarkable theorem to the e¤ect that there is a power of 2 containing any given number of consecutive zeros.
Cli¤or A. Pickover, in A passion for mathematics, page 247, says that ac-cording to Michael Beeler and William Gosper, there is at least one zero in each power of 2 between 286 = 77 371 252 455 336 267 181 195 264, and 230749014. See item 57 of www.inwap.com/pdp10/hbaker/hakmen/hakmem.html.
There is also a proof there, by Rich Schroeppel, that you can get arbitrarily many nonzero …nal digits (in fact, 1’s and 2’s).
To look for patterns in the digits of a number, you can convert it to a string, and then use the Python function …nd. If s and t are strings, then s.…nd(t) will return the …rst index in the string s where the pattern t starts. So if s is the string ’23050007900’, then s.…nd(’00’) will return 4. If the pattern t does not appear in the string s, then s.…nd(t) returns 1. So s.…nd(’1’) will return 1 for this string s. To convert a number into a string, use the Python function str. For example, str(1024) returns the string ’1024’.
Exercise 1. Verify that 2377 is the smallest power of 2 containing four consecutive zeros.
Exercise 2. Verify that the other numbers in the table are correct. The number 14007 might take some time.
Exercise 2. Explain why 21492 contains six consecutive zeros. Remember this on Columbus Day!
Exercise 3. Find all powers of 2 less than 2200 that contain no zeros.
14 Ciphers
A standard method for enciphering a piece of text is to replace the letters by other letters according to a …xed scheme. One famous example is attributed to Julius Caesar. The idea was to replace the letter a by the letter d, the letter b by the letter e, the letter c by the letter f , and so on until the letter z is replaced by the letter c. The scheme can be summarized in the following table
a b c d e f g h i j k l m n o p q r s t u v w x y z d e f g h i j k l m n o p q r s t u v w x y z a b c where the letters in the second row are substituted for the letters in the …rst row. So the word “caesar”would become “fdhvu”. If we think of the letters of the alphabet as being the numbers 0; 1; 2; : : : ; 25, this amounts to replacing the number n by the number n + 3 (mod 26).
More brie‡y, we can think of the plain alphabet as being the string
"abcdefghijklmnopqrstuvwxyz" and what we need to do is to specify a cipher alphabet which is some permutation of these 26 letters. In the Caesar cipher case, we choose the cipher alphabet to be the string "defghijklmnopqrstu-vwxyzabc".
Suppose p is the plain alphabet and c is the cipher alphabet. How do we encrypt a message t? We want to form a new string r which is the encrypted message. The i-th character in the message t is t [i].
We need to know the position j of t [i] in the plain alphabet p. (Remember that positions start at 0.) Then we can set r [i] equal to c [j]. There is a Python function associated with the string p that does just that. It is called p.…nd(). If we write p.…nd(’u’), we will get the position of the …rst occurrence of the letter u in the string p. If the letter u does not appear in the string p,
We need to know the position j of t [i] in the plain alphabet p. (Remember that positions start at 0.) Then we can set r [i] equal to c [j]. There is a Python function associated with the string p that does just that. It is called p.…nd(). If we write p.…nd(’u’), we will get the position of the …rst occurrence of the letter u in the string p. If the letter u does not appear in the string p,