I've written a sketch that is supposed to generate prime numbers using the modulo operator, but I'm having trouble with it printing anything to the serial monitor. My goal is for it to be as fast as possible, so I'm trying to make it simple to achieve maximum speed. I set the serial communication at 115200 baud on a nano, which should work, although the only thing that comes to mind is that it has a different driver chip than most arduinos (CH340). Here's my code:
/*
Prime Number Generator
This program starts from the number 3 and prints all
the primes occuring after it up until 4,294,967,295.
*/
long x = 3; // this variable will contain the number to be tested
long z = 1; // this variable will increment to indicate how many primes have been found
long y; // this variable will be the divisor that checks if the number is prime
long start; // this variable holds the microseconds at the beginning
boolean prime; // this variable indicates if the number that was tested is prime
void setup(){
Serial.begin(115200); // initalize serial communication
}
void loop(){
start = micros();
do{
for(int y = 3; (y * y) < x; y += 2){
if(x % y == 0){
prime = false;
break;
}
}
x += 2;
}while(!prime);
Serial.print(x); // prints the next prime number
Serial.print("\t\t"); // prints two spaces
Serial.print(z); // prints the number of prime numbers generated
Serial.print("\t\t"); // prints a space
z++;
Serial.println(micros() - start); // prints the time in microseconds the calculation took
}