{"id":61,"date":"2010-03-01T22:15:35","date_gmt":"2010-03-01T21:15:35","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=61"},"modified":"2010-05-10T13:39:45","modified_gmt":"2010-05-10T12:39:45","slug":"recursion-with-factorial","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/recursion-with-factorial\/","title":{"rendered":"Recursion with factorial"},"content":{"rendered":"<p>A recursive function is a procedure or subroutine, implemented in a programming language, whose implementation references itself.<br \/>\nIt is really useful, try to make a program like <a href=\"http:\/\/en.wikipedia.org\/wiki\/Towers_of_Hanoi\">Tower of Hanoi<\/a> with an iterative algorithm&#8230; good luck !<\/p>\n<p>So I will give you an easy method to create a recursive algorithm, I choose the example of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Factorial\">factorial<\/a> because of it simplicity.<br \/>\nMathematical&#8217;s reminder : 4! = 4 * 3 * 2 * 1 = 24.<\/p>\n<p>There are 3 steps :<br \/>\n&#8211; parameter(s) : identify all the parameters that you need to resolve the problem. They will decrease at each call of the function. In the factorial&#8217;s case, it is the factorial&#8217;s number.<\/p>\n<pre lang=\"actionscript3\">function factorialCalculation(factorial:uint):uint<\/pre>\n<p>&#8211; exception&#8217;s case : this is when the procedure stops to call itself.<\/p>\n<pre lang=\"actionscript3\">if (factorial == 1) { return 1; }<\/pre>\n<p>&#8211; recursive&#8217;s case : where recursive call is implemented, don&#8217;t forget to decrease (generally) parameter(s).<\/p>\n<pre lang=\"actionscript3\">return factorial * factorialCalculation(factorial - 1);<\/pre>\n<p>Now all the code :<\/p>\n<pre lang=\"actionscript3\">var factorial:uint = 4;\r\nvar resul:uint = 0;\r\n\r\nresul = factorialCalculation(factorial);\r\n\r\nfunction factorialCalculation(factorial:uint):uint {\r\n\r\n\tif (factorial == 1) {\r\n\t\treturn 1;\r\n\t} else {\r\n\t\treturn factorial*factorialCalculation(factorial-1);\r\n\t}\r\n}<\/pre>\n<p><strong>Be careful : recursive function are really heavy. Factorial&#8217;s case is just an easy example, it should be implemented as an interative function !<br \/>\n<\/strong><br \/>\nAnd for the fun with a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ternary_operation\">ternary operation<\/a> :<\/p>\n<pre lang=\"actionscript3\">function factorialCalculation(factorial:uint):uint {\r\n\r\n\treturn (factorial == 1) ? 1 : factorial * factorialCalculation(factorial - 1);\r\n}<\/pre>\n<p>\ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A recursive function is a procedure or subroutine, implemented in a programming language, whose implementation references itself. It is really useful, try to make a program like Tower of Hanoi with an iterative algorithm&#8230; good luck ! So I will give you an easy method to create a recursive algorithm, I choose the example of &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/recursion-with-factorial\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Recursion with factorial<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/61"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":11,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":951,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/61\/revisions\/951"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}