{"componentChunkName":"component---src-components-blogpost-blogpost-jsx","path":"/blog/alg1/","result":{"data":{"site":{"siteMetadata":{"title":"K011"}},"mdx":{"id":"88083ce2-55b3-5c7e-b424-05cdfa9b6020","body":"var _excluded = [\"components\"];\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Recursion\",\n  \"date\": \"2021-08-26\",\n  \"description\": \"Algorithm recursion\"\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Recursion.\"), mdx(\"h2\", null, \"What is recursion?\"), mdx(\"p\", null, \"Recursion consists of functions that call themsellves, avoiding the use of loops and other iterators.  That is to say when you call the function it call\"), mdx(\"center\", null, mdx(\"img\", {\n    src: \"https://wewrite.taleas.io/wp-content/uploads/2018/11/cnnv-nv-768x614.jpg\"\n  })), mdx(\"h3\", null, \"Factorial\"), mdx(\"p\", null, \"A good way to understand recursion and how it works is with examples and a good example is the factorial\"), mdx(\"h3\", null, \"Examples:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, \"def factorial(n):\\n    if n==1:\\n        return 1\\n    else:\\nreturn factorial(n-1)*n\\n\")), mdx(\"p\", null, \"What this does is that when having the factorial it calls the function again and again but each time being the smaller number. If the number reaches 1 it returns 1. so this 1 is multiplied by each one of the times and the numbers by which it passed until arriving to the last one.\"), mdx(\"h3\", null, \"Fibonacci\"), mdx(\"p\", null, \"Another great example is the fibonacci function.\"), mdx(\"p\", null, \"the fibonacci sequence is a sequence which starts with 0 and 1. And to find more numbers that belong to the sequence you will have to add the 2 previous numbers, ie.\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"0,1,1,2,3,5\")), mdx(\"p\", null, \"since 0+1 is 1 and in position 4 it would be 1+1 which is 2 so formally the number of position n would be the number of position n-2 plus the number of position n-1\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, \"def fibo(n):\\n   if n <= 1:\\n       return n\\n   else:\\n       return(fibo(n-1) + fibo(n-2))\\n\")), mdx(\"h2\", null, mdx(\"strong\", {\n    parentName: \"h2\"\n  }, \"PROS AND CONS\")), mdx(\"p\", null, \"As you can see recursion is easy to understand but if one looks at it more closely one can see that everything can be done with cycles. and in some cases it is more efficient. for example the case of the factorial would be:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, \"def factorial(n):\\n    r=1\\n        for i in range(2,n):\\n            r= i*r\\n    return r    \\n\")), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Then why do we use recursive functions?\")), mdx(\"p\", null, \"One of the main reasons is because it is more readable and dry.\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Why not use them\")), mdx(\"p\", null, \"Most programming languages every time a function is called inside another function it is executed in something called a stack.\"), mdx(\"p\", null, \"When the stack fills up (stack overflow) it can generate the closing of the program so that for uses that require a very high cycle it is generally better to use cycles\"), mdx(\"p\", null, \"A solution for this is \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"tail call optimization\")), mdx(\"h2\", null, \"When to use recursion\"), mdx(\"p\", null, \"Recursion is very useful when there is no simple iterative solution. For example,complicated problems like traversing or searching for a node in a graph or a tree, or sorting by merge sort or quick sort.\"), mdx(\"p\", null, \"A very interesting thing is that divide and conquer is considered as recursion.\"));\n}\n;\nMDXContent.isMDXComponent = true;","frontmatter":{"title":"Recursion","date":"2021-08-26","description":"Algorithm recursion"}},"allMdx":{"edges":[{"node":{"frontmatter":{"title":"Classification","date":"2021-11-03","description":"Naive Bayes, Discriminant Analysis, Logistic Regression"},"fields":{"slug":"/blog/est4/"}}},{"node":{"frontmatter":{"title":"Regression and Prediction","date":"2021-11-02","description":"REGRESSION"},"fields":{"slug":"/blog/est3/"}}},{"node":{"frontmatter":{"title":"Distribution","date":"2021-11-01","description":"Data distributions"},"fields":{"slug":"/blog/est1/"}}},{"node":{"frontmatter":{"title":"Stats Testing","date":"2021-11-01","description":"Statistical Experiments and significance testing"},"fields":{"slug":"/blog/est2/"}}},{"node":{"frontmatter":{"title":"Recursion","date":"2021-08-26","description":"Algorithm recursion"},"fields":{"slug":"/blog/alg1/"}}}]}},"pageContext":{"slug":"/blog/alg1/","id":"88083ce2-55b3-5c7e-b424-05cdfa9b6020"}},"staticQueryHashes":["63159454"]}