{"id":38318,"date":"2016-10-25T20:16:25","date_gmt":"2016-10-26T01:16:25","guid":{"rendered":"http:\/\/hectormtzblog.wordpress.com\/?p=656"},"modified":"2016-10-25T20:16:25","modified_gmt":"2016-10-26T01:16:25","slug":"recursion-2","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/recursion-2\/","title":{"rendered":"Recursion"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"https:\/\/hectormtzblog.wordpress.com\/2016\/10\/25\/recursion\/\"><a href=\"https:\/\/hectormtzblog.wordpress.com\">Hector Martinez Alcantara<\/a><\/a><\/em><br \/><\/p>\n<p>Let&#8217;s talk about recursion. Personally, recursion is the \u00a0most dificult thing in programming, but I&#8217;ll try to explain it by the most common recursive function, the factorial.<\/p>\n<p>Let&#8217;s see the example code:<\/p>\n<pre>def factorial(var):\n    if var&gt;1:\n       var=var*factorial(var-1)\n       return var\n    else:\n       return 1\n \nx=int(input(\"Type a number to calculate the factorial:\\n\"))\nprint(\"The factorial of \"+str(x)+\" is:\")\nprint(factorial(x))<\/pre>\n<p>The result of this is:<\/p>\n<pre>Type a number to calculate the factorial:\n5\nThe factorial of 5 is:\n120<\/pre>\n<p>But why?<\/p>\n<p>Let&#8217;s see step by step, what happens.<\/p>\n<p>First of all, we call the function with one parameter.<\/p>\n<pre>factorial(5) #factorial of 5<\/pre>\n<p>The number 5 enters to the function then if the number is 1 it returns 1, and then, the interesting part, if there&#8217;s some number higher than 1 the next function will be realized<\/p>\n<pre>var=var* factorial(var-1)<\/pre>\n<p>What happen here? Let&#8217;s see step by step.<\/p>\n<p>First the value is 5, it&#8217;s greater than 1, so the function will be<\/p>\n<pre>var= 5 * factorial(var-1)<\/pre>\n<p>Now we can see that the value of factorial var-1 will be the same function, so we can see that the value of var in the function will be 4 that part will be like this:<\/p>\n<pre>var= 5 * (4*factorial((var-1)-1)<\/pre>\n<p>As you can see the value of the function factorial is the same function nested in the other sentence, so I&#8217;m goin to show you how the function looks doing the same procedure.<\/p>\n<pre>var=5 * ( 4 * ( 3 * ( 2 * (factorial ((((var-1)-1)-1)-1) ) ) ) )\n#Remember the var==5\n#((((var-1)-1)-1)-1) is like ((((5-1)-1)-1)-1) == 1<\/pre>\n<p>So&#8230;<\/p>\n<pre>\nvar=5 * ( 4 * ( 3 * ( 2 * (factorial(1) ) ) )<\/pre>\n<p>What happens with factorial of 1? Remember that we typed that if the variable of the function is 1 or less, then the function will return <div class=\"post-limited-image\"><img src=\"https:\/\/i2.wp.com\/images.firstcovers.com\/covers\/flash\/m\/me_against_the_world-816910.jpg\" \/><\/div>\n<div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/hectormtzblog.wordpress.com\/656\/\" \/><\/div>\n<div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=hectormtzblog.wordpress.com&#038;blog=115238241&%23038;post=656&%23038;subd=hectormtzblog&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/><\/div><!--more--> number 1. So the operation will be seen like this.<\/p>\n<pre>var=5 * ( 4 * ( 3 * ( 2 * (1) ) ) )<\/pre>\n<p>That&#8217;s equal to 5*4*3*2*1. So the result is equal to 120.<\/p>\n<p>Now you know how it works, your challenge now is to know how to use it.<\/p>\n<p><img src=\"https:\/\/i2.wp.com\/images.firstcovers.com\/covers\/flash\/m\/me_against_the_world-816910.jpg\" \/><\/p>\n<p>Thanks for reading, any doubt in comments.<\/p>\n<p>Special thanks to oadiazr from\u00a0<a href=\"https:\/\/www.taringa.net\/post\/ciencia-educacion\/15913940\/Factorial-Recursivo-lenguaje-C.html\" >Taringa&#8217;s post<\/a>.<\/p><br \/>  <a rel=\"nofollow\" href=\"http:\/\/feeds.wordpress.com\/1.0\/gocomments\/hectormtzblog.wordpress.com\/656\/\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/hectormtzblog.wordpress.com\/656\/\" \/><\/a> <img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=hectormtzblog.wordpress.com&#038;blog=115238241&%23038;post=656&%23038;subd=hectormtzblog&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at Hector Martinez Alcantara Let&#8217;s talk about recursion. Personally, recursion is the \u00a0most dificult thing in programming, but I&#8217;ll try to explain it by the most common recursive function, the factorial. Let&#8217;s see the example code: def factorial(var): if var&gt;1: var=var*factorial(var-1) return var else: return 1 x=int(input(&#8220;Type a number to calculate the factorial:\\n&#8221;)) &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/recursion-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Recursion<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":38319,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[88,89,44,26,33],"class_list":["post-38318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","category-syndicated","tag-learninganewlanguage","tag-python3","tag-loops","tag-sin-categoria","tag-tc101"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=38318"}],"version-history":[{"count":1,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38318\/revisions"}],"predecessor-version":[{"id":38320,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38318\/revisions\/38320"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media\/38319"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=38318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=38318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=38318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}