Little useless-useful R functions – Goldbach’s conjecture and Sieve of Sundaram

This is fun 🙂 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’s conjecture states that for every even integer (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)).

To put this to test, we created a simple Prime function, to determine if integer is a prime or not.


# sieve of sundaram
sieve_of_sundaram <- function(limit) {
  n <- (limit - 1) %/% 2
  sieve <- rep(TRUE, n + 1)
  
  for (i in 1:n) {
    j <- 1
    while (i+j+2*i*j <= n) {
      sieve[i+j+2*i*j] <- FALSE
      j <- j + 1
    }
  }
  primes <- c(2,(2*(1:n)+1)[sieve])
  return(primes)
}

# is prime
is_prime <- function(n) {
  if (n <= 1)  return(FALSE)
  if (n <= 3)  return(TRUE)
  if (n %% 2 == 0 || n %% 3 == 0) return(FALSE)
  i <- 5
  while (i*i <= n) {
    if (n %% i == 0 || n %% (i + 2) == 0) return(FALSE)
    i <- i + 6
  }
  return(TRUE)
}
  

For fun, I am adding the famous Sieve of Sundaram algorithm for finding multiple primes in an array of integers.

For the next step, we find sum of all two primes for every even number as input; with other words, the Goldbach’s conjecture.

goldbach_conjecture <- function(even_num) {
  if (even_num <= 2 || even_num %% 2 != 0) {
    return("Number must be even and greater than 2.")
  }
  c <- NULL
  for (i in 2:(even_num / 2)) {
    if (is_prime(i) && is_prime(even_num - i)) {
      #cat("Goldbach's pairs for", even_num, "are:", i, "+", even_num - i, "\n")
      c <- cbind(c,i) # nof solutions
    } 
  }
  return(length(c))
}

But not to end here, let’s put the Goldbach’s conjecture to test and see how the solutions are generated:

#make some 1000 solutions
sol <- NULL
for (i in seq(4,1000, by=2)){
  nof_solutions <- goldbach_conjecture(i)
  sol <- rbind(sol, data.frame(n=i, nof=nof_solutions))
}

# plot solutions; alternating solutions
plot(sol$n, sol$nof, type = "p", xlab = "Even number", ylab = "Number of Solutions", main = "Goldbach's Conjecture") 
reg<-lm(nof ~ n, data = sol)
abline(reg, col="red")

We see the “alternating” pattern of solutions for every even number between 4 and 1000 as sum of two primes.

And the distribution of prime frequencies is equally “interesting” 🙂

As always, code is available on Github in  Useless_R_function repository. The sample file in this repository is here (filename: Goldbach_conjecture.R) Check the repository for future updates.

Happy R-coding and stay healthy!

Tagged with: , , , , , ,
Posted in R, Useless R functions
2 comments on “Little useless-useful R functions – Goldbach’s conjecture and Sieve of Sundaram
  1. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

  2. […] Tomaz Kastrun promised us there would be no math on the quiz and yet here we are: […]

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...