Arduino calculate prime numbers

I wrote some code to stress test my Arduino. The code below will make your Arduino calculate prime numbers.

long start       = 0;
int  max_seconds = 30;
long i           = 2; // Start at 2
long found       = 0; // Number of primes we've found

void setup() {
    Serial.begin(115200);

    while (!Serial) { }

    Serial.println("");
    Serial.print("Running prime benchmark for ");
    Serial.print(max_seconds);
    Serial.println(" seconds...");

    start = millis();
}

void loop() {
    int prime = is_prime(i); // Check if the number we're on is prime

    if (prime == 1) {
        found++;

        if (found % 2000 == 0) {
            Serial.print(".");
        }

        if (found % 50000 == 0) {
            Serial.print("\n");
        }
    }

    int running_seconds = (millis() - start) / 1000;

    if (max_seconds > 0 && (running_seconds >= max_seconds)) {
        Serial.print("\nFound ");
        Serial.print(found);
        Serial.print(" primes in ");
        Serial.print(max_seconds);
        Serial.println(" seconds");
        delay(10000);

        i     = 2;
        found = 0;
        start = millis();
    }

    i++;
}

int is_prime(long num) {
    // Only have to check for divisible for the sqrt(number)
    int upper = sqrt(num);

    // Check if the number is evenly divisible (start at 2 going up)
    for (long cnum = 2; cnum <= upper; cnum++) {
        long mod = num % cnum; // Remainder

        if (mod == 0) {
            return 0;
        } // If the remainer is 0 it's evenly divisible
    }

    return 1; // If you get this far it's prime
}
Leave A Reply - 1 Reply
Replies
random 2022-02-28 11:43am - No Email - Logged IP: 2003:e0:71b:8800:758c:cfb0:6762:1ca5

Should be 3 times faster

uint32_t start = 1; uint32_t max_seconds = 10; uint32_t i = 11; // Start at 11 uint16_t found = 4; // Number of primes we've found, start with 4 as there are 4 primes prior 11

void setup() { Serial.begin(115200);

while (!Serial) { }

Serial.println(""); Serial.print("Prime benchmark for "); Serial.print(max_seconds); Serial.println(" seconds...");

start = millis(); }

void loop() {

is_prime(); // to cancel out all numbers modulo 2 or 3 = 0 i+=2; // Odd + 2 = odd is_prime(); i+=4; //9%3=0 so 11+2+2 would be %3 = 0 so we add here 4 instead of 2 //we could filter out other Prime dividables as 5,7,... but this woud be a mess here

if (millis() >= (start + ((max_seconds)*1000))) { Serial.print("nFound "); Serial.print(found); Serial.print(" primes in "); Serial.print(max_seconds); Serial.println(" seconds"); Serial.print(" searched to "); Serial.print(i-4); delay(5000);

i     = 11;
found = 4;
start = millis();

}

}

boolean is_prime() { // could be void, but my esp did not like it so here with boolean // Only have to check for divisible for the sqrt(number) uint32_t upper = sqrt(i);

// Check if the number is evenly divisible (start at 2 going up) for (uint32_t cnum = 5; cnum <= upper; cnum+=4) { //Same +2 ... +4 trick as before

if (((i % cnum) == 0)) {
  return 0;
}
cnum+=2;
if (((i % cnum) == 0)) {
  return 0;
}

}

found++; //Serial.print(i); //Serial.print("n"); return 1; // If you get this far it's prime }

All content licensed under the Creative Commons License