Number theory has always interested me, specifically irrational numbers and prime numbers. I was messing around with functions in R and came up with a very neat ‘one line’ function in R that checks if a number is prime. See below:
(Bonus Math Karma to anyone that knows why the square root function is in there). To check the function works, here are the first 17 integers checked:
Looks good so far. This ‘IsPrime’ function checks integers for being prime. I thought to myself, what are the reaches of this function? How big of a number can I check with reasonable times/resources? Let’s create a sequence of very large numbers.
Note that the vector ‘prime.seq.check’ contains the sequence:
100,001; 1,000,001; 10,000,001; …
And so on, and ends with a 15 digit integer:
100,000,000,000,001 (A hundred billion and one). What are the times to compute if these are prime? Let’s use the ‘proc.time’ R-function to loop through and time these.
Let’s plot the output:
Looks very exponential. Let’s fit an exponential function using some basic algebra.
Turns out the function is given by f(x) = (4.082583E-6) * exp(1.220295 * x). The largest prime to date has 17,425,170 digits. I used Wolfram Alpha to calculate this, and it would take about 2E9234766 seconds. MUCH longer than the estimated age of the universe.
So, needless to say, this prime number function is not going to help in the search for prime numbers. But it is cool.
More props to you, pure math.