1 00:00:01,040 --> 00:00:02,540 PROFESSOR: Our method for estimating 2 00:00:02,540 --> 00:00:06,200 sums can also be used to estimate products, basically 3 00:00:06,200 --> 00:00:09,377 by taking logs to turn a product into a sum. 4 00:00:09,377 --> 00:00:10,960 And we're going to use that to come up 5 00:00:10,960 --> 00:00:13,220 with another important estimate of a quantity that 6 00:00:13,220 --> 00:00:17,310 will come up, really, very regularly called n factorial. 7 00:00:17,310 --> 00:00:22,090 S n factorial is the product of the first n integers, 1 times 8 00:00:22,090 --> 00:00:24,240 2 up through n minus 1 times n. 9 00:00:24,240 --> 00:00:26,120 In concise product notation, it's 10 00:00:26,120 --> 00:00:29,710 the product-- that's pi, capital pi, for product-- from i 11 00:00:29,710 --> 00:00:31,970 equals 1 to n of i. 12 00:00:31,970 --> 00:00:35,070 And its standard abbreviation is to write it as n! 13 00:00:35,070 --> 00:00:37,660 pronounced n factorial. 14 00:00:37,660 --> 00:00:41,010 So what I'd like to do is get an asymptotic estimate 15 00:00:41,010 --> 00:00:41,740 for n factorial. 16 00:00:41,740 --> 00:00:43,865 Again, n factorial is one of these quantities where 17 00:00:43,865 --> 00:00:46,800 there isn't any exact formula that doesn't 18 00:00:46,800 --> 00:00:48,880 have those ellipses in it. 19 00:00:48,880 --> 00:00:53,280 There's no short formula with basic operations 20 00:00:53,280 --> 00:00:56,450 fixed size of formula that expresses n factorial. 21 00:00:56,450 --> 00:01:01,070 But we get a nice formula for a tight asymptotic estimate. 22 00:01:01,070 --> 00:01:05,850 So as I said, the first trick is to turn the product into a sum 23 00:01:05,850 --> 00:01:07,270 by taking logs. 24 00:01:07,270 --> 00:01:10,890 So log of n factorial is the log of 1-- the product of 1 25 00:01:10,890 --> 00:01:11,390 through n. 26 00:01:11,390 --> 00:01:14,420 But a log of a product is the sum of the logs, 27 00:01:14,420 --> 00:01:19,100 so it's simply log of 1 plus log of 2 up through log of n. 28 00:01:19,100 --> 00:01:21,920 And expressed in sum notation, it's 29 00:01:21,920 --> 00:01:25,870 the sum from i equals 1 to n of log of i. 30 00:01:25,870 --> 00:01:28,180 Now, the integral method gives us 31 00:01:28,180 --> 00:01:31,940 a way to estimate this sum by bracketing it 32 00:01:31,940 --> 00:01:34,770 between the values of some integrals, 33 00:01:34,770 --> 00:01:38,630 namely restating the integral method for sum-- 34 00:01:38,630 --> 00:01:40,730 for bounding integrals by sums. 35 00:01:40,730 --> 00:01:42,980 This time, we're looking at an increasing function 36 00:01:42,980 --> 00:01:46,080 because it's log of x. 37 00:01:46,080 --> 00:01:50,690 Let f be a weakly increasing function from positive reals 38 00:01:50,690 --> 00:01:51,720 the positive reals. 39 00:01:51,720 --> 00:01:55,830 I'm interested in the sum from i equals 1 to n of f of i. 40 00:01:55,830 --> 00:01:58,220 And I want to relate it and bound it by the integral 41 00:01:58,220 --> 00:02:02,650 from 0-- from 1 to n of f of x where, in this case, 42 00:02:02,650 --> 00:02:04,410 the particular f that we're interested in 43 00:02:04,410 --> 00:02:07,430 is f of x is log x. 44 00:02:07,430 --> 00:02:11,150 And the theorem says that with increasing functions 45 00:02:11,150 --> 00:02:14,400 s is bracketed between the integral plus the last term 46 00:02:14,400 --> 00:02:17,560 in the sum and the integral plus the first term in the sum. 47 00:02:17,560 --> 00:02:20,680 Remember, since the function is weakly increasing, f of 1 48 00:02:20,680 --> 00:02:22,240 is smaller than f of n. 49 00:02:22,240 --> 00:02:25,440 So that's the way you remember which way the bounds go. 50 00:02:25,440 --> 00:02:29,570 So s is between I plus f of 1 and I plus f of n 51 00:02:29,570 --> 00:02:33,980 by our general formula for applying integral bounds 52 00:02:33,980 --> 00:02:35,760 to sums. 53 00:02:35,760 --> 00:02:39,340 Well, what that tells us then is that the sum from 1 to n 54 00:02:39,340 --> 00:02:41,300 of log of i, which is what we're interested in, 55 00:02:41,300 --> 00:02:43,550 is bracketed between the integral from 1 to n 56 00:02:43,550 --> 00:02:49,100 of log x and the integral from 1-- well, it's plus log of 0, 57 00:02:49,100 --> 00:02:51,750 but-- log of 1 rather, but that's 0. 58 00:02:51,750 --> 00:02:55,270 And the integral from 1 to n of log of x plus the last term, 59 00:02:55,270 --> 00:02:58,520 which is log of n. 60 00:02:58,520 --> 00:03:01,470 In case you don't remember from first term calculus, 61 00:03:01,470 --> 00:03:05,120 the integral of log of x is, in fact-- 62 00:03:05,120 --> 00:03:07,890 has the indefinite integral is x log of x over e, 63 00:03:07,890 --> 00:03:12,620 which you can easily check by differentiating x log x over e. 64 00:03:12,620 --> 00:03:15,420 ln means natural log, remember. 65 00:03:15,420 --> 00:03:18,920 In computer science, L-O-G, log means log to the base 2 66 00:03:18,920 --> 00:03:21,380 unless you explicitly put some base on it 67 00:03:21,380 --> 00:03:23,700 like log, L-O-G, sub 10. 68 00:03:23,700 --> 00:03:26,390 So ln is the natural log from calculus. 69 00:03:26,390 --> 00:03:31,800 And plugging in this value for the indefinite integral of log 70 00:03:31,800 --> 00:03:35,360 of x and using the bounds 1, n, what we come up 71 00:03:35,360 --> 00:03:40,480 with is that the sum of the logs is bounded between n times log 72 00:03:40,480 --> 00:03:45,950 n over e and n times log n over e plus log of n. 73 00:03:45,950 --> 00:03:47,840 It's a pretty tight bounds. 74 00:03:47,840 --> 00:03:50,130 What that means is that informally speaking, 75 00:03:50,130 --> 00:03:54,350 the sum of the logs is about this term plus that term. 76 00:03:54,350 --> 00:03:56,990 Plus, let's take the average value of that term, which 77 00:03:56,990 --> 00:03:58,620 is half this term. 78 00:03:58,620 --> 00:04:02,110 So we could say that the sum of logs is approximately equal. 79 00:04:02,110 --> 00:04:04,560 That's a little vague, but live with it. 80 00:04:04,560 --> 00:04:08,790 n log n over e plus half of log n. 81 00:04:08,790 --> 00:04:10,430 Well, now, if I'm interested, remember, 82 00:04:10,430 --> 00:04:12,690 in an estimate for n factorial-- so 83 00:04:12,690 --> 00:04:15,230 let's exponentiate both sides. 84 00:04:15,230 --> 00:04:19,839 So taking e to this sum gives me a product of e 85 00:04:19,839 --> 00:04:21,980 to this times e to that. 86 00:04:21,980 --> 00:04:27,830 Well, e to this is-- really, it's e to the log of n over e 87 00:04:27,830 --> 00:04:32,010 to the nth power, which means it's n over e to the n. 88 00:04:32,010 --> 00:04:36,920 And this is e to the log of n to the power half, 89 00:04:36,920 --> 00:04:39,070 or square root of n. 90 00:04:39,070 --> 00:04:41,510 So we wind up with n factorial is 91 00:04:41,510 --> 00:04:44,760 approximately equal to the square root of n times n over e 92 00:04:44,760 --> 00:04:45,260 to the n. 93 00:04:45,260 --> 00:04:48,360 Now, this approximately equal is imprecise. 94 00:04:48,360 --> 00:04:51,260 It's not asymptotically equal because we 95 00:04:51,260 --> 00:04:54,860 were doing an arithmetic average of 0 and log n over 2. 96 00:04:54,860 --> 00:04:56,680 In addition, it's very dangerous when 97 00:04:56,680 --> 00:04:58,430 you have two things that are approximately 98 00:04:58,430 --> 00:05:01,580 equal to exponentiate them and expect that they're still 99 00:05:01,580 --> 00:05:02,600 approximately equal. 100 00:05:02,600 --> 00:05:03,740 Often, they aren't. 101 00:05:03,740 --> 00:05:07,200 But nevertheless, this is a kind of a heuristic derivation 102 00:05:07,200 --> 00:05:10,410 of some kind of asymptotic estimate 103 00:05:10,410 --> 00:05:12,810 that we would expect that n factorial was roughly 104 00:05:12,810 --> 00:05:16,600 like the square root of n times n over e to the nth power. 105 00:05:16,600 --> 00:05:19,770 And it turns out that it's-- that this heuristic gives 106 00:05:19,770 --> 00:05:21,450 a pretty accurate answer. 107 00:05:21,450 --> 00:05:24,380 A precise approximation is that n factorial is actually 108 00:05:24,380 --> 00:05:28,910 asymptotically equal to the square root of 2 pi n times 109 00:05:28,910 --> 00:05:30,334 n over e to the n. 110 00:05:30,334 --> 00:05:31,750 And we're not going to prove that. 111 00:05:31,750 --> 00:05:34,890 It requires elementary calculus, but more than we 112 00:05:34,890 --> 00:05:36,960 want to take time for. 113 00:05:36,960 --> 00:05:40,500 And this crucial formula that we will be using very regularly 114 00:05:40,500 --> 00:05:42,780 to estimate the size of n factorial 115 00:05:42,780 --> 00:05:45,420 is called Stirling's Formula, and it's 116 00:05:45,420 --> 00:05:50,190 one to have on your crib sheets if you haven't memorized it.