The world would probaly be a better plece without it.....
// Prime generator
// Crude and dirty
const unsigned long primeMax = 10000;
unsigned long i, j;
boolean prime;
void setup(){
Serial.begin(9600);
Serial.println(2);
for (i = 3; i < primeMax; i=i+2){
prime = true;
for (j = 3; j < sqrt(i)+1; j=j+2){
if (i%j == 0){
prime = false;
break;
}
}
if (prime == true) Serial.println(i);
}
}
void loop(){}
-Fletcher
Update - code performance:
primeMax set to 30000
Normal code: 29.1 sec
sqrt(i)+1 outside loop: 27.7 sec
sqrt(i)+1 and change to unsigned int: 22.7 sec
Serial changed to 115200: 6.78 sec ..... much faster
That was the main thing I learned when I wrote my first prime number generator; that actually outputting the numbers was very expensive compared to computing them. (for a six-digit prime number, you're looking at a couple thousand instructions to test its primeness, max. But each digit output to my terminal was probably a similar number of instructions within the system call, plus context switches between operating system and user process (this was on a mainframe), plus actually dealing with a relatively complex IO device, plus, plus...
Last mod -- removed call to sqrt() and replaced it with code to keep track
of when 'i' got to or passed a new square (1,4,9,16,25, etc). If this
happens I bump the Jmax and bumped to the next square. Easy to do
when you know how to figure what the next square is. Got my time down
to 5913-mSec.
Need to replace the (i%j == 0) to get any more speed.
Almost like the superPI program to do benchmarks on the computer. how long do you think it might take an arduino to calculate one million digits of pi? ;D
The original code took around 40 min to 1/2 million.
Humm, since Rusty is improving with some slick square optimizing I better look into this again :o
Memory is rather limitet but it should be possible to store the first 500 primes (as unsigned integer ~ 1 kb ) in a list and divide a prime candidate with the list before change to sieveing.