{"id":12637,"date":"2021-09-12T23:02:13","date_gmt":"2021-09-13T06:02:13","guid":{"rendered":"https:\/\/portasftpserver.com\/?p=12637"},"modified":"2021-11-20T19:44:51","modified_gmt":"2021-11-21T03:44:51","slug":"play-collatz-conjecture-with-r","status":"publish","type":"post","link":"https:\/\/portasftpserver.com\/play-collatz-conjecture-with-r\/","title":{"rendered":"R: Play With Collatz Conjecture"},"content":{"rendered":"\n<p class=\"sera-block-paragraph\">We all know that programming is fun but sometimes it can also be boring. In this blog let&#8217;s see how can we use the &#8220;Simple&#8221; but uncrackable math problem called <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Collatz_conjecture\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Collatz_conjecture\" target=\"_blank\">Collatz Conjecture<\/a>. &#8220;The conjecture is named after&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Lothar_Collatz\">Lothar Collatz<\/a>, who introduced the idea in 1937&#8243; (Wikipedia, Sept. 13, 2021, para. 2).<\/p>\n\n\n\n<blockquote class=\"sera-block-quote is-layout-flow sera-block-quote-is-layout-flow\"><p>The&nbsp;<strong>Collatz conjecture<\/strong>&nbsp;is a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Conjecture\">conjecture<\/a>&nbsp;in&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Mathematics\">mathematics<\/a>&nbsp;that concerns&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Sequence\">sequences<\/a>&nbsp;defined as follows: start with any&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Positive_integer\">positive integer<\/a>&nbsp;<em>n<\/em>. Then each term is obtained from the previous term as follows: if the previous term is&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Parity_(mathematics)\">even<\/a>, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of&nbsp;<em>n<\/em>, the sequence will always reach 1.<\/p><cite>Wikipedia<\/cite><\/blockquote>\n\n\n\n<p class=\"sera-block-paragraph\">So, the question is what exactly are we going to do here? Well as I&#8217;ve said, we want to have fun with this simple but uncrackable problem with <a rel=\"noreferrer noopener\" href=\"https:\/\/www.r-project.org\/\" data-type=\"URL\" data-id=\"https:\/\/www.r-project.org\/\" target=\"_blank\">R<\/a> using <a href=\"https:\/\/www.rstudio.com\/products\/rstudio\/download\/\" data-type=\"URL\" data-id=\"https:\/\/www.rstudio.com\/products\/rstudio\/download\/\">R Studio<\/a>. The goal here is to find the conjecture of a number using the Collatz Conjecture formula that if <em>n=odd<\/em> then multiplied by 3 plus 1 and if <em>n=even<\/em> divided by two until the number reaches 1. Who knows, you might encounter this as a coding test in your next job.<\/p>\n\n\n\n<p class=\"sera-block-paragraph\">So if you are following this blog, I presumed that you have or know how to install the tools above. The steps that we are going to do are the following:<\/p>\n\n\n\n<ul class=\"sera-block-list\"><li>Declare global variable as holder to count how many step(s) before reaching the number 1<\/li><li>Create a function to initialize Collatz Conjecture calculations<\/li><li>Create a funtion to check if the integer reaches number one<\/li><li>Create a function to multiple the number by 3 plus one if the given value is odd<\/li><li>Create a function for division if the given value is even<\/li><\/ul>\n\n\n\n<h3 class=\"sera-block-heading\">Declare global variable<\/h3>\n\n\n\n<p class=\"sera-block-paragraph\">This variable will be incremented every step will pass during calculations<\/p>\n\n\n\n<pre class=\"sera-block-code\"><code lang=\"python\" class=\"language-python\">step &lt;- 0 #global variable<\/code><\/pre>\n\n\n\n<h3 class=\"sera-block-heading\"><span style=\"text-decoration: underline;\">Collatz Conjecture<\/span> calculations<\/h3>\n\n\n\n<p class=\"sera-block-paragraph\">In this function, you can see that the value is being checked if it&#8217;s Odd or Even, then do the required calculation based on the conditions. Since this will be called frequently, you can also see that this will increment the global variable to count the <em>step<\/em>s.<\/p>\n\n\n\n<pre class=\"sera-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">CollatzConjecture &lt;- function(value) {\n\n  assign(\"step\", step + 1, envir = .GlobalEnv) # Steps\/counts handler\n   \n  isOdd &lt;- (value %% 2 == 1)\n  \n  if (isOdd) {\n    \n    print(paste(\"Odd value is:\", value,sep = \" \",collapse = NULL))\n    \n    return(CheckIfOne(ThreeNPlusOne(value)))\n      \n  }\n  else\n  {\n    print(paste(\"Even value is: \", value,sep = \" \", collapse = NULL))\n    \n    return(CheckIfOne(DivideByTwo(value)))\n  }\n  \n}<\/code><\/pre>\n\n\n\n<h3 class=\"sera-block-heading\">Function that check if reaches the number 1<\/h3>\n\n\n\n<p class=\"sera-block-paragraph\">This is the function needed to be executed to check if the given value is 1. If the value is not one then do other recursive Collatz Conjecture calculations.<\/p>\n\n\n\n<p class=\"sera-block-paragraph\">In addition, we do not want to proceed any further if <em>n<\/em>=1, otherwise it will be on an infinite loop from 4 to 1.<\/p>\n\n\n\n<pre class=\"sera-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">CheckIfOne &lt;- function(value) {\n     \n  if (value != 1) {\n      \n      return(CollatzConjecture(value))\n    \n  }else{\n      return(paste(\"Reached #:\", value,\"Count Of Steps:\", step,sep = \" \",collapse = \"\"))\n  }\n  \n}<\/code><\/pre>\n\n\n\n<h3 class=\"sera-block-heading\">Function for <em><strong>3n + 1<\/strong><\/em> if <em><strong>n<\/strong><\/em> is Odd<\/h3>\n\n\n\n<pre class=\"sera-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">ThreeNPlusOne &lt;- function(value) {\n\n  result&lt;-((value * 3) + 1)\n\n  print(paste(\"3 *\", value, \"+ 1=\", result, sep = \" \",collapse = NULL))\n\n  return(result)\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"sera-block-heading\">Function for division if <em><strong>n<\/strong><\/em> is even<\/h3>\n\n\n\n<pre class=\"sera-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">DivideByTwo &lt;- function(value) {\n\n  result&lt;-(value\/2)\n\n  print(paste(value, \"\/ 2 =\", result, sep = \" \", collapse = NULL))\n\n  return(result)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"sera-block-heading\">Sample usage<\/h3>\n\n\n\n<pre class=\"sera-block-code\"><code lang=\"python\" class=\"language-python\">CollatzConjecture(71)<\/code><\/pre>\n\n\n\n<p class=\"sera-block-paragraph\">Sample result, Note that not all are posted as there are 102 steps.<\/p>\n\n\n\n<pre class=\"sera-block-code\"><code lang=\"python\" class=\"language-python\">[1] \"Odd value is: 53\"\n[1] \"3 * 53 + 1= 160\"\n[1] \"Even value is:  160\"\n[1] \"160 \/ 2 = 80\"\n[1] \"Even value is:  80\"\n[1] \"80 \/ 2 = 40\"\n[1] \"Even value is:  40\"\n[1] \"40 \/ 2 = 20\"\n[1] \"Even value is:  20\"\n[1] \"20 \/ 2 = 10\"\n[1] \"Even value is:  10\"\n[1] \"10 \/ 2 = 5\"\n[1] \"Odd value is: 5\"\n[1] \"3 * 5 + 1= 16\"\n[1] \"Even value is:  16\"\n[1] \"16 \/ 2 = 8\"\n[1] \"Even value is:  8\"\n[1] \"8 \/ 2 = 4\"\n[1] \"Even value is:  4\"\n[1] \"4 \/ 2 = 2\"\n[1] \"Even value is:  2\"\n[1] \"2 \/ 2 = 1\"\n[1] \"Reached #: 1 Count Of Steps: 102\"<\/code><\/pre>\n\n\n\n<p class=\"sera-block-paragraph\">And that&#8217;s it, as you can see with simple and neat coding, you can get the Collatz Conjecture outcomes of any positive integers. Now, it is time for you to do it, improve or put some graphing. Hope you enjoy it, keep learning, and happy coding.<\/p>\n\n\n\n<p class=\"sera-block-paragraph\">You can validate the output of our work here <a href=\"https:\/\/www.dcode.fr\/collatz-conjecture\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.dcode.fr\/collatz-conjecture<\/a><\/p>\n\n\n\n<p class=\"sera-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all know that programming is fun but sometimes it can also be boring. In this blog let&#8217;s see how can we use the &#8220;Simple&#8221; but uncrackable math problem called Collatz Conjecture. &#8220;The conjecture is named after&nbsp;Lothar Collatz, who introduced the idea in 1937&#8243; (Wikipedia, Sept. 13, 2021, para. 2). The&nbsp;Collatz conjecture&nbsp;is a&nbsp;conjecture&nbsp;in&nbsp;mathematics&nbsp;that concerns&nbsp;sequences&nbsp;defined as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[142,206,1],"tags":[207],"class_list":["post-12637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science-analytics","category-learning-educations","category-programming-software","tag-r-with-collatz-conjecture"],"_links":{"self":[{"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/posts\/12637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/comments?post=12637"}],"version-history":[{"count":0,"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/posts\/12637\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/media\/12641"}],"wp:attachment":[{"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/media?parent=12637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/categories?post=12637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/portasftpserver.com\/sera-json\/wp\/v2\/tags?post=12637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}