2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97


Sums of Primes in R

I was interested in the Weak Goldbach Conjecture:
Any odd integer greater than 5 can be expressed as a sum of 3 prime numbers.

Namely some very interesting graphs are out there about the actual number of combinations of primes that yield odd numbers. Now, this exercise was made hastily but it gives a good foundation of further analysis.
The Weak Goldbach Conjecture specifies odd integers, but I used any number that can be expressed as a sum of primes (within a range). One thing to note, even numbers that can be expressed as a sum of three primes must have 2 as one of the primes. Simply: odd plus odd is even, add a third odd and you yield and odd number. 2 is the only even prime. Therefore it probably isn't hard to find an even integer that cannot be expressed as a sum of three primes.
Here is a generated histogram showing bins of integers and the numbers of ways they could be expressed as a sum of three primes *from our sample*.
Interestingly, integers between 1 and 200 have a great difference in expressions than anything past 200.
Two reasons could be why. It could be that even numbers start disappearing from possible combination outcomes, and/or that could be the cutoff from our sample. If you want that question answered, some code can be found below ;^)
About the sample limitations: I used the first 100 primes. (I actually started with the first 1000 primes but the cross product of the set used up all my RAM and crashed R. too many chrome tabs). The 100th prime is 541, therefore the sum of 3 primes ~500 is ~3000, seen on the final bar on the graph above. The 400th prime is 2741. That means there are more than 300 more primes whose value is below 3000, and it excludes a lot of possible prime combinations up to that point. In fact, any finite amount of primes used in sum combinations will have a cut off property because the value of the prime function grows much larger than n+1 (the integer function).
A very skewed distribution is expected, but we can see some interesting flow to the histogram, specifically how small the bar at 2250 is. I could hypothesize something about twin primes creating a lot more prime in the range of the twin primes plus many primes within the 0-100 range, where they are very dense.
I pulled a list of primes off the internet, but one generate their own. They could create a list of integers, use the crossing() function like I did to create every pair of integer, then transform() to create a product of them, finally use anti_join() to remove every integer that is a product of two or more integers. This is probably not a very good job for use of R, but would work.

follow me on twitter @kevgk2 for more blog updates!

code below
#import primes. I put it in a csv file
#I had to create a dataframe from it since it was imported as a vector
primes<-data.frame(primes)

#create new column with new name (inefficient way rather than colnames(), idk why)
primes<-transform(primes, p=...1)

#replicate prime columns in same dataframe
primes<-transform(primes, p2=p)
primes<-transform(primes, p3=p)

#focus on first 100 primes rather than thousand I imported, and columns 2,3,4
subprimes<-primes[1:100,2:4]
View(subprimes)

#use tidyr package crossing() to create each unique list of prime combinations
primecombo<-tidyr::crossing(subprimes$p,subprimes$p2,subprimes$p3)
View(primecombo)

#rename columns (can't remember why)
names(primecombo)<-c("p","p2","p3")

#create sum of primes
primecombo<-transform(primecombo, sums=p+p2+p3)

#count number of unique sums that yield each number
primecounts<-as.data.frame(table(primecombo$sums))
View(primecounts)

#plot histogram
plot(hist(primecounts$Freq))

Comments

Popular posts from this blog

Profiling 2019 NFL Offenses with nflscrapR Data and Clustering

Using the Excel Nonlinear Solver to Optimize Skill Trees with Borderlands 3 Example

Jordan Love Was The Right Pick In Theory