Code Monkey Want Cookie
673 words | Posted on June 5th, 2009
Scott was 23.70 years old when he wrote this!
Filed under: General, Prime Numbers, Python
This week has been a hectic week of ups and downs, with the stress never letting up. Work is stressful (I’m being challenged to collect representative data to write & submit two manuscripts for publication in JCN ASAP) and home is stressful. My wife and I are down to one car right now, which has no AC and pumps out heat! There’s always stuff I need to do around the house (cleaning, cooking, laundry, etc). Bills are actually getting a little intimidating since I haven’t gotten paid accurately in over a month and a half due to a payroll problem at UCF. On top of everything, I only have a few minutes here and there to devote to my microcontroller projects. When I have time to work on them, I’m rushed, and often make bad mistakes (such a yesterday when I destroyed 6 of my 2222 NPN transistors).
Thankfully, I’ve had a couple lucky breaks lately. Yesterday I finished the hardware enclosure and wiring for my microcontroller-powered prime number generator prototype. The code is sufficient to blink each of the 30 LEDs individually. When I do it fast, they all appear on. I’m golden – all I need to do to finish the [prototype] project is software side. Time to refresh myself on how to pass arrays with pointers in C! In other news, I had a little mental breakthrough this afternoon allowing me to generate prime numbers significantly more sufficiently. The screenshot below shows me identifying the 2,364,346′th prime number (38,790,019) in a little under two and a half hours. Yay! [makes dinner]

After a few hours running the code described above (writing down some time points along the way) I decided to try and visualize the curve of the rate of primes discovered with respect to time. I figured it would resemble an inverse exponential curve but it was surprisingly linear. Before I go to bed tonight I’ll re-write the code to make it automatically log time points so I can graph it in higher accuracy tomorrow. For now, this is what it looks like:

As much as I’d like to toot my own horn about how fast and efficient my computational method is. The linear estimate based upon this data (an overshoot of the actual speed) suggests that it finds 10,000 prime numbers per minute. That sounds lovely, but according to my math in order to get to the 10^12′th prime (the goal) it would take 190 years. I hope I have a full battery.
The code to generate the graph (requires MatPlotLib):
data="""
112.5,2062841
141.0,2364346
168.75,2656766
177.5,2745573
214.5,3123373
232.5,3290106
247.5,3419679
256.5,3493450
"""
import pylab
x,y,fx,fy=[],[],[],[]
for line in data.split('n'):
if not "," in line: continue
nx,ny = line.split(",")
x.append(float(nx))
y.append(float(ny))
pylab.figure(figsize=(8,4))
pylab.plot(x,y,'k.-',label='real data',lw=1)
pylab.plot([x[0],x[-1]],[y[0],y[-1]],'b:',label='linear estimate')
pylab.ylabel("number of primes found")
pylab.xlabel("time (minutes)")
pylab.title("pyPrime Nth Prime Speed (bin=[2,3,5])")
pylab.legend(loc=8)
pylab.grid(alpha=.2)
pylab.show()
After letting the program run overnight using a larger prime bin (primes=[2,3,5,7]) and recording the progress every 100 bins I have a better idea of the speed (and rate of decay) of this method.

This entry was posted on Friday, June 5th, 2009 at 8:51 pmand is filed under General, Prime Numbers, Python. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.
