#define MAX_PRIMES 300
int primes[MAX_PRIMES];
int prime_count = 0;
int is_prime(int num) {
for (int i = 0; i < prime_count; i++) {
// No need to check beyond the square root of the number
if (primes[i] * primes[i] > num) {
break;
}
if (num % primes[i] == 0) {
return 0; // Return 0 (false) if not prime
}
}
return 1; // Return 1 (true) if prime
}
void prime_uart() {
printUartNewLine();
printUartNewLine();
printUartNewLine();
int n = 1;
primes[0] = 2;
prime_count = 1;
while(1){
if(GPIO_READ == 0xAA000000) return;
if(GPIO_READ == 0x55000000) {
delay(1);
while(GPIO_READ != 0x00000000);
int next_prime = primes[prime_count - 1] + 1; // Start checking from the next number
while (prime_count < n) {
if (is_prime(next_prime)) {
primes[prime_count] = next_prime; // Store the found prime
prime_count++; // Increment the count of primes found
}
next_prime++; // Check the next number
}
printUart(primes[n - 1]); // Return the nth prime
printUartSpace();
n++;
}
}
}