{"id":287,"date":"2007-01-23T21:05:03","date_gmt":"2007-01-23T21:05:03","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2007\/01\/23\/basics-recursion-and-induction\/"},"modified":"2007-01-23T21:05:03","modified_gmt":"2007-01-23T21:05:03","slug":"basics-recursion-and-induction","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2007\/01\/23\/basics-recursion-and-induction\/","title":{"rendered":"Basics: Recursion and Induction"},"content":{"rendered":"<p> Time for another sort-of advanced basic. I used some recursive definitions in my explanation<br \/>\nof <a href=\"http:\/\/scientopia.org\/blogs\/goodmath\/2007\/01\/basics-natural-numbers-and-integers\">natural numbers and integers<\/a>. Recursion is a very fundamental concept, but one which many people have a very hard time wrapping their head around.  So it&#8217;s worth taking the time to look at it, and see what it means and how it works.<\/p>\n<p> The cleverest definition that I&#8217;ve seen of recursion comes from <a href=\"http:\/\/www.worldwideschool.org\/library\/books\/tech\/computers\/TheHackersDictionaryofComputerJargon\/chap43.html\">the Hackers dictionary.<\/a> In there, it has:<\/p>\n<pre>\n<b>recursion<\/b>\nn. See {recursion}.\n<\/pre>\n<p><!--more--><\/p>\n<p> Recursion is about defining things in terms of themselves. For what is probably <em>the<\/em> canonical example, think about the factorial function. For any positive integer N, the factorial of N (written N!) is the product of all of the integers less than it:<\/p>\n<ul>\n<li> 1! = 1<\/li>\n<li> 2! = 1 &times; 2 = 2<\/li>\n<li> 3! = 1 &times; 2 &times; 3 = 6<\/li>\n<li> 4! = 1 &times; 2 &times; 3 &times; 4 = 24<\/li>\n<li> 5! = 1 &times; 2 &times; 3 &times; 4 &times; 5 = 120<\/li>\n<li> &#8230;\n<\/ul>\n<p> But if you look at it, you&#8217;ll see that that&#8217;s a cumbersome way of saying it &#8211; because there&#8217;s a pattern. Written out as they are above, you can see that each number&#8217;s factorial <em>includes<\/em> the factorial of all of the numbers before it &#8211; 2! is &#8220;1 &times; 2&#8221;; &#8220;3! = 1 &times; 2 &times; 3&#8221; &#8211; so 3! is the same as 2! &times; 3. And it works that way for <em>every<\/em> number: for any number <em>N<\/em> greater than 1,<br \/>\n<em>N! = (N-1)! &times; N<\/em>. Now, let&#8217;s use that fact to make the list simpler:<\/p>\n<ul>\n<li> 1! = 1<\/li>\n<li> 2! = 1 &times; 2 = 2<\/li>\n<li> 3! = 2 &times; 3 = 6<\/li>\n<li> 4! = 6 &times; 4 = 24<\/li>\n<li> 5! = 24 &times; 5 = 120<\/li>\n<li> &#8230;<\/li>\n<li> N! = (N-1)! &times; N<\/li>\n<\/ul>\n<p> So that&#8217;s the first piece of recursion: a definition <em>in terms of itself<\/em>: the definition of the factorial of a number N is defined in terms of the factorial of some other number.<\/p>\n<p> So can we say that in general, N! = (N-1)! &times; N?<\/p>\n<p> Not quite.  Let&#8217;s take a look at why. Let&#8217;s pretend that we <em>could<\/em> use that, and try to compute 3!: 3! = 3 &times; 2! = 3 &times; 2 &times; 1! = 3 &times; 2 &times; 1 &times; 0! = 3 &times; 2 &times; 1 &times; 0 &amp;times -1! &#8230; = 0. <\/p>\n<p> We&#8217;ve run into two problems. One of them is that as a procedure, it <em>never finishes<\/em>. There&#8217;s <em>always<\/em> another <em>N-1<\/em> &#8211; you can always subtract 1 from any number &#8211; and since we said that for <em>any<\/em> number, N! = (N-1)!&times;N, that means that we need to keep going forever. The second is that for any positive number, it will always give the answer 0 &#8211; because <em>any<\/em> string of multiplications containing zero will always end up returning 0, and repeatedly subtracting one from any positive number will eventually get to zero.<\/p>\n<p> To make recursion work, you need something more than just a definition in terms of itself. A definition in terms of itself with nothing more is just a circle; you need something base to start from, some point, so that instead of being an endless circle,<br \/>\nit eventually reaches an end. That starting point is called a <em>base case<\/em>. <\/p>\n<p> For the factorial, the way that computer scientists like me do that is say that the factorial of 0 is 1 <em>by definition<\/em>. Zero is the base case, and the value of the factorial is defined <em>non-recursively<\/em> for the base case. So then our definition of factorial consists of two clauses: the base case, and the recursive case: <\/p>\n<ul>\n<li><b>Base case<\/b>: 0! = 1<\/li>\n<li><b>Recursive case<\/b>: &forall; N &gt; 0, N! = (N-1)! &times; N.<\/li>\n<\/ul>\n<p>With this definition, things work as they should. factorial is only supposed to work for positive numbers; and for any positive number, the recursive definition will expand until it hits 0!, and then it will stop. So let&#8217;s look at 3! again:<\/p>\n<p> 3! = 2! &times; 3 = 1! &times; 2 &times; 3 = 0! &times; 1 &times; 2 &times; 3 = 1 &times; 1 &times; 2 &times; 3 = 6.<\/p>\n<p> Recursion is often used in math in another way: often, one of the easiest ways to prove something is called induction; induction is nothing but using recursion to define a proof.<\/p>\n<p> For a very typical example, we can look at something similar to the factorial. What<br \/>\nif, for some natural number <em>N<\/em>, we want to take the sum of every number from 1 to N? We&#8217;ll write that as S(N). We can write a simple recursive definition of S(N):<\/p>\n<ul>\n<li> S(0)=0<\/li>\n<li> For all N &gt; 0, S(N) = S(N-1)+N.<\/li>\n<\/ul>\n<p> It happens that there&#8217;s also a non-recursive equation for it: the sum of all of the integers from 1 to N = S_N = N&times;(N+1)\/2. Here&#8217;s how we can prove that.<\/p>\n<p> We start with a base case. We only need one, but just for the exercise, let&#8217;s work<br \/>\nthrough two examples.<\/p>\n<ul>\n<li> By the recursive definition, S(1) = S(0) + 1 = 0 + 1 = 1.<br \/>\nBy the equation, S(1) = 1 &times; (1+1)\/2 = 1&times;2\/2 = 1.<\/li>\n<li> By the recursive definition, S(4) = 0 + 1 + 2 + 3 + 4 = 10. By the equation,<br \/>\nS(4) = 4&times;(4+1)\/2 = 4&times;5\/2 = 20\/2 = 10<\/li>\n<\/ul>\n<p> Now we look at the inductive case. We assume that the equation is true for a value N, and we prove that if it&#8217;s true for N, then it will also be true for N+1. So, we assume that S(N) = N&times;(N+1)\/2; and we want to prove that S(N+1) = (N+1)&times;(N+2)\/2.<\/p>\n<ol>\n<li> We know that S(N+1) = (N+1) + S(N).<\/li>\n<li> We know S(N) by our assumption. So S(N+1) = (N+1) + (N&times;(N+1)\/2)<\/li>\n<li> Now, we do a bit of algebra to expand out and simplify: S(N+1) = (N+1) + (N<sup>2<\/sup>+N)\/2.<\/li>\n<li> We can multiply (N+1) by 2\/2 to give it a common denominator, and then add<br \/>\nthe two terms together: 2(N+1)\/2 + (N<sup>2<\/sup> + N)\/2 = (N<sup>2<\/sup> + 2N +2)\/2.<\/li>\n<li> And finally, we can factor: S(N) = (N<sup>2<\/sup> + 2N +2)\/2 = (N+1)&times;(N+2)\/2.<\/li>\n<\/ol>\n<p> So, we&#8217;ve shown that for the base case of 1, the equation holds. By induction, if it&#8217;s true for 1, it&#8217;s true for 2. If it&#8217;s true for 2, it&#8217;s true for three. And so on&#8230; So it holds for <em>all<\/em> of the natural numbers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time for another sort-of advanced basic. I used some recursive definitions in my explanation of natural numbers and integers. Recursion is a very fundamental concept, but one which many people have a very hard time wrapping their head around. So it&#8217;s worth taking the time to look at it, and see what it means and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[74,24],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-basics","category-goodmath"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-4D","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}