{"id":320,"date":"2011-06-10T20:54:47","date_gmt":"2011-06-10T20:54:47","guid":{"rendered":"http:\/\/craftydba.com\/?p=320"},"modified":"2024-02-17T14:26:07","modified_gmt":"2024-02-17T14:26:07","slug":"recursive-programs","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=320","title":{"rendered":"Recursive Programs"},"content":{"rendered":"<p>I would like to chat about one of my favorite programming topics, implementing recursive algorithms.<\/p>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Recursion_(computer_science)\">Recursion<\/a> in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem. A recursive program is a program that calls itself to work on a smaller sub problem. When the stop condition (last problem) is reached, all results are put together to reach the desired result. The tricky part is to make sure that the program does not execute in a infinite call loop.<\/p>\n<p>I am going to show you how to write a recursive program to perform a summation of the numbers n to m. There are two types of recursion (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Recursion_(computer_science)#Tail-recursive_functions\">tail<\/a> or regular) that can be used to solve the problem.<\/p>\n<p>Tail recursion is when the program passes the intermediate results to the next function call. When the stop condition is reached, the result is fully calculated and just returned off the function call stack. Some compilers are designed to handle this fact and do not incure the costs of stack space to hold the intermediate calls since they are of no value.<\/p>\n<p>Regular recursion is when the program is waiting for a result to be returned from the next function call. With this type of recursion, the result is only fully realized when the first call finishes the final calculation.<\/p>\n<p>The function below solves the summation problem with tail recursion by counting down from m to n on sub calls.<\/p>\n<pre class=\"lang:VB theme:familiar mark:1,2-3\" title=\"vb script - sumsbytail()\">\r\nFunction SumsByTail(n, m, v)\r\n\u00a0\u00a0if (m < n) then SumsByTail = v\r\n\u00a0\u00a0else SumsByTail = SumsByTail (n, m - 1, m + v)\r\nEnd Function\r\n<\/pre>\n<p>The function below solves the summation problem with regular recursion by counting up from n to m on sub-calls.<\/p>\n<pre class=\"lang:VB theme:familiar mark:1,2-3\" title=\"vb script - sumsbyregular()\">\r\nFunction SumsByRegular(n, m)\r\n\u00a0\u00a0if (n > m) then SumsByRegular = 0\r\n\u00a0\u00a0else SumsByRegular = n + SumsByRegular (n + 1, m)\r\nEnd Function\r\n<\/pre>\n<p>It has been mathematically proven that recursive algorithms can be re-written as iterative ones. Constructs such as the FOR or WHILE loop are used in such solutions. However, the solutions are not as elligant as a recursive one.<\/p>\n<p>The function below solves the summation problem by using a loop construct.<\/p>\n<pre class=\"lang:VB theme:familiar mark:1,2-3\" title=\"vb script - sumsbyloop()\">\r\nFunction SumsByLoop(n, m)\r\n\u00a0\u00a0SumsByLoop = 0\r\n\u00a0\u00a0while (n <= m)\r\n\u00a0\u00a0\u00a0\u00a0SumsByLoop = SumsByLoop + n\r\n\u00a0\u00a0\u00a0\u00a0n = n + 1\r\n\u00a0\u00a0wend\r\nEnd Function\r\n<\/pre>\n<p>The bottom line is that recursive programs take longer to execute than iterative ones. I ran the three alogirthms above to give me a summation from 5 to 1500. The table below shows the run times.<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" width=\"400\" align=\"left\">\n<tbody>\n<tr>\n<td style=\"border: thin solid gray;\">SumsByTail()<\/td>\n<td style=\"border: thin solid gray;\">0.234375 secs<\/td>\n<td style=\"border: thin solid gray;\">122.22 % more time<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">SumsByRegular()<\/td>\n<td style=\"border: thin solid gray;\">0.1875 secs<\/td>\n<td style=\"border: thin solid gray;\">77.77 % more time<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">SumsByLoop()<\/td>\n<td style=\"border: thin solid gray;\">0.1054688 secs<\/td>\n<td style=\"border: thin solid gray;\">baseline time<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/P><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>In short, recursive programs use the runtime stack to save context information of each function call which can consume memmory. For large problems, one might run out of memmory due to this fact unless tail recursion with a smart compiler is used. However, the compactness of these algorithms make them appealing and fun to write.<\/p>\n<p>If you are interested in this topic, trying writing programs for the Fibannoci Sequence, Factorial Expressions, or the Tower of Hannoi problem.<\/p>\n<p>As usual, here is the sample code that was used to create recursive summation programs.<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" width=\"600\" align=\"left\">\n<tbody>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/prg-calc-sums.vbs_.txt\">prg-calc-sums.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Recursive Program For Summations<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/sums-ouput.txt\">sums-output.txt<\/a><\/td>\n<td style=\"border: thin solid gray;\">Sample Program Output<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/sums.cmd.txt\">sums.cmd<\/a><\/td>\n<td style=\"border: thin solid gray;\">Run Sample Program<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>I would like to chat about one of my favorite programming topics, implementing recursive algorithms. Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem. A recursive program is a program that calls itself to work on a smaller sub problem. When the stop condition (last problem) is reached, all results are put together to reach the desired result. The tricky part is to make sure that the program does not execute in a infinite call loop.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[12,15,22,13],"class_list":["post-320","post","type-post","status-publish","format-standard","hentry","category-other","tag-free-code","tag-john-f-miner-iii","tag-recursive-algorithms","tag-vb-script-2"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=320"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/320\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}