{"id":345888,"date":"2023-08-12T13:04:42","date_gmt":"2023-08-12T19:04:42","guid":{"rendered":"http:\/\/tomaztsql.wordpress.com\/?p=9304"},"modified":"2023-08-12T13:04:42","modified_gmt":"2023-08-12T19:04:42","slug":"little-useless-useful-r-functions-goldbachs-conjecture-and-sieve-of-sundaram","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2023\/08\/little-useless-useful-r-functions-goldbachs-conjecture-and-sieve-of-sundaram\/","title":{"rendered":"Little useless-useful R functions \u2013 Goldbach\u2019s conjecture and Sieve of Sundaram"},"content":{"rendered":"<!-- \r\n<div style=\"min-height: 30px;\">\r\n[social4i size=\"small\" align=\"align-left\"]\r\n<\/div>\r\n-->\r\n\r\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\">\r\n[This article was first published on  <strong><a href=\"https:\/\/tomaztsql.wordpress.com\/2023\/08\/12\/little-useless-useful-r-functions-goldbachs-conjecture-and-sieve-of-sundaram\/\"> R \u2013 TomazTsql<\/a><\/strong>, and kindly contributed to <a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers<\/a>].  (You can report issue about the content on this page <a href=\"https:\/\/www.r-bloggers.com\/contact-us\/\">here<\/a>)\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>\n\n<p>This is fun <img src=\"https:\/\/i1.wp.com\/s0.wp.com\/wp-content\/mu-plugins\/wpcom-smileys\/twemoji\/2\/72x72\/1f642.png?w=578&#038;ssl=1\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" data-recalc-dims=\"1\" \/> It is also O(MAX) complexity. But first some background. Since the problem is super old, we are not intending to solve it, merely to play with it. In the number theory of mathematics, the Goldbach\u2019s conjecture states that for <strong>every even integer<\/strong> (greater than 2) can be expressed with the sum of two prime numbers. There are also far cries from this theory. For example, prove that every even number can be written as the sum of not more than 300.000 primes (by Schnirelman (1939)).<\/p>\n\n\n\n<p>To put this to test, we created a simple Prime function, to determine if integer is a prime or not.<\/p>\n\n\n<pre>\n\n# sieve of sundaram\nsieve_of_sundaram &lt;- function(limit) {\n  n &lt;- (limit - 1) %\/% 2\n  sieve &lt;- rep(TRUE, n + 1)\n  \n  for (i in 1:n) {\n    j &lt;- 1\n    while (i+j+2*i*j &lt;= n) {\n      sieve[i+j+2*i*j] &lt;- FALSE\n      j &lt;- j + 1\n    }\n  }\n  primes &lt;- c(2,(2*(1:n)+1)[sieve])\n  return(primes)\n}\n\n# is prime\nis_prime &lt;- function(n) {\n  if (n &lt;= 1)  return(FALSE)\n  if (n &lt;= 3)  return(TRUE)\n  if (n %% 2 == 0 || n %% 3 == 0) return(FALSE)\n  i &lt;- 5\n  while (i*i &lt;= n) {\n    if (n %% i == 0 || n %% (i + 2) == 0) return(FALSE)\n    i &lt;- i + 6\n  }\n  return(TRUE)\n}\n  \n<\/pre>\n\n\n<p>For fun, I am adding the famous Sieve of Sundaram algorithm for finding multiple primes in an array of integers.<\/p>\n\n\n\n<p>For the next step, we find sum of all two primes for every even number as input; with other words, the Goldbach\u2019s conjecture.<\/p>\n\n\n<pre>\ngoldbach_conjecture &lt;- function(even_num) {\n  if (even_num &lt;= 2 || even_num %% 2 != 0) {\n    return(&quot;Number must be even and greater than 2.&quot;)\n  }\n  c &lt;- NULL\n  for (i in 2:(even_num \/ 2)) {\n    if (is_prime(i) &#038;&#038; is_prime(even_num - i)) {\n      #cat(&quot;Goldbach's pairs for&quot;, even_num, &quot;are:&quot;, i, &quot;+&quot;, even_num - i, &quot;\\n&quot;)\n      c &lt;- cbind(c,i) # nof solutions\n    } \n  }\n  return(length(c))\n}\n\n<\/pre>\n\n\n<p>But not to end here, let\u2019s put the Goldbach\u2019s conjecture to test and see how the solutions are generated:<\/p>\n\n\n<pre>\n#make some 1000 solutions\nsol &lt;- NULL\nfor (i in seq(4,1000, by=2)){\n  nof_solutions &lt;- goldbach_conjecture(i)\n  sol &lt;- rbind(sol, data.frame(n=i, nof=nof_solutions))\n}\n\n# plot solutions; alternating solutions\nplot(sol$n, sol$nof, type = &quot;p&quot;, xlab = &quot;Even number&quot;, ylab = &quot;Number of Solutions&quot;, main = &quot;Goldbach's Conjecture&quot;) \nreg&lt;-lm(nof ~ n, data = sol)\nabline(reg, col=&quot;red&quot;)\n<\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png\" rel=\"nofollow\" target=\"_blank\"><img data-attachment-id=\"9315\" data-permalink=\"https:\/\/tomaztsql.wordpress.com\/2023\/08\/12\/little-useless-useful-r-functions-goldbachs-conjecture-and-sieve-of-sundaram\/image-41\/\" data-orig-file=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png\" data-orig-size=\"2142,1092\" data-comments-opened=\"1\" data-image-meta=\"{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png?w=300\" data-large-file=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png?w=605\" src=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png?w=578\" alt=\"\" class=\"wp-image-9315\" srcset_temp=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png?w=578 1024w, https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png?w=2048 2048w, https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png?w=150 150w, https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png?w=300 300w, https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image.png?w=768 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><\/a><\/figure><\/div>\n\n\n<p>We see the \u201calternating\u201d pattern of solutions for every even number between 4 and 1000 as sum of two primes.<\/p>\n\n\n\n<p>And the distribution of prime frequencies is equally \u201cinteresting\u201d <img src=\"https:\/\/i1.wp.com\/s0.wp.com\/wp-content\/mu-plugins\/wpcom-smileys\/twemoji\/2\/72x72\/1f642.png?w=578&#038;ssl=1\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" data-recalc-dims=\"1\" \/><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png\" rel=\"nofollow\" target=\"_blank\"><img data-attachment-id=\"9318\" data-permalink=\"https:\/\/tomaztsql.wordpress.com\/2023\/08\/12\/little-useless-useful-r-functions-goldbachs-conjecture-and-sieve-of-sundaram\/image-1-13\/\" data-orig-file=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png\" data-orig-size=\"2344,818\" data-comments-opened=\"1\" data-image-meta=\"{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}\" data-image-title=\"image-1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png?w=300\" data-large-file=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png?w=605\" src=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png?w=578\" alt=\"\" class=\"wp-image-9318\" srcset_temp=\"https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png?w=578 1024w, https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png?w=2046 2046w, https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png?w=150 150w, https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png?w=300 300w, https:\/\/tomaztsql.files.wordpress.com\/2023\/08\/image-1.png?w=768 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><\/a><\/figure><\/div>\n\n\n<p>As always, code is available on Github in \u00a0<a rel=\"nofollow\" href=\"https:\/\/github.com\/tomaztk\/Useless_R_functions\" target=\"_blank\">Useless_R_function repository<\/a>. The sample file in this repository is\u00a0<a rel=\"nofollow\" href=\"https:\/\/github.com\/tomaztk\/Useless_R_functions\/blob\/main\/functions\/Goldbach_conjecture.R\" target=\"_blank\">here<\/a>\u00a0(filename:\u00a0<em>Goldbach_conjecture.R<\/em>) Check the repository for future updates.<\/p>\n\n\n\n<p>Happy R-coding and stay healthy!<\/p>\n\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\">\r\n<div style=\"text-align: center;\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"https:\/\/tomaztsql.wordpress.com\/2023\/08\/12\/little-useless-useful-r-functions-goldbachs-conjecture-and-sieve-of-sundaram\/\"> R \u2013 TomazTsql<\/a><\/strong>.<\/div>\r\n<hr \/>\r\n<a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers.com<\/a> offers <strong><a href=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=RBloggers\" rel=\"nofollow\">daily e-mail updates<\/a><\/strong> about <a title=\"The R Project for Statistical Computing\" href=\"https:\/\/www.r-project.org\/\" rel=\"nofollow\">R<\/a> news and tutorials about <a title=\"R tutorials\" href=\"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/\" rel=\"nofollow\">learning R<\/a> and many other topics. <a title=\"Data science jobs\" href=\"https:\/\/www.r-users.com\/\" rel=\"nofollow\">Click here if you're looking to post or find an R\/data-science job<\/a>.\r\n\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<div style = \"width:60%; display: inline-block; float:left; \"> This is fun \ud83d\ude42 It is also O(MAX) complexity. But first some background. Since the problem is super old, we are not intending to solve it, merely to play with it. In the number theory of mathematics, the Goldbach\u2019s conjecture\u2026Read more \u203a<\/div>\n<div style = \"width: 40%; display: inline-block; float:right;\"><\/div>\n<div style=\"clear: both;\"><\/div>\n","protected":false},"author":1281,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"aioseo_notices":[],"jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/345888"}],"collection":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/users\/1281"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=345888"}],"version-history":[{"count":23,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/345888\/revisions"}],"predecessor-version":[{"id":383346,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/345888\/revisions\/383346"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=345888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=345888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=345888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}