{"id":74,"date":"2009-07-31T13:16:21","date_gmt":"2009-07-31T13:16:21","guid":{"rendered":"http:\/\/www.themissingdocs.net\/wordpress\/?p=74"},"modified":"2009-07-31T13:16:21","modified_gmt":"2009-07-31T13:16:21","slug":"loops-and-recursion","status":"publish","type":"post","link":"https:\/\/www.themissingdocs.net\/?p=74","title":{"rendered":"Loops and recursion"},"content":{"rendered":"<p>I was recently asked to write a simple algorithm using recursion.  I immediately baulked because the algorithm was so trivial and recursion was such a bad choice.<br \/>\nBut for novelty sake i wrote it up on the whiteboard.  It was wrong, I didn&#8217;t put the termination condition in, but I don&#8217;t program whiteboards anyway and the fact that the problem was so stupid to solve as recursion had me befuddled.<\/p>\n<p>But it has made me think.  Think about how bad the solution I wrote up was.  I was asked to write a recursive solution, to a linear single loop problem, so I wrote it thinking like I was solving a problem which was best solved by recursion.  Solution ran n^2, I knew it, was the biggest reason why I thought writing the algorithm with recursion was stupid.<br \/>\nLater I was thinking, it wouldn&#8217;t be that hard to make the recursion linear, its still a terrible way to solve the problem but at least its still linear.<\/p>\n<p>But it was only just now that the obvious truth hit me, you can turn any loop into a recursion trivially without changing the asymptotic performance (your performance will likely drop, and you&#8217;ll probably stack overflow, but the O notation won&#8217;t change).  I had been so focused on the request for recursion that I didn&#8217;t do what I should have done, which is solve the problem the right way, and then make that fit the requirements. (Not that that is a good motto for general development&#8230;)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently asked to write a simple algorithm using recursion. I immediately baulked because the algorithm was so trivial and recursion was such a bad choice. But for novelty sake i wrote it up on the whiteboard. It was wrong, I didn&#8217;t put the termination condition in, but I don&#8217;t program whiteboards anyway and &hellip; <a href=\"https:\/\/www.themissingdocs.net\/?p=74\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Loops and recursion<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-random-musings"],"_links":{"self":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":0,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}