Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LANGUAGE ARDUINO WITH ARDUINO... The problem: Prime number Build a program that

ID: 3742060 • Letter: L

Question

 LANGUAGE ARDUINO WITH ARDUINO... The problem: Prime number Build a program that allows to read through the serial port a whole number. The program You must determine if the number provided is a prime number or not. NOTE: A prime number is that number that is only divisible between itself and the unit. By example 3, 5, 7, 11 are prime numbers. As an example, only those cases, but there are more prime numbers and the goal is that you build a program that helps determine this task. Points to consider: a) The program should include the function called bool PRIME (int x) that calculates whether the parameter x is or is not a prime number. The function returns true if x is prime, false if contrary. b) Call the sketch CheckPrime .

Explanation / Answer

ANSWER:

int count = 2; //Start at 2- 1 doesn't count
int printDelay = 1000; //Minimum prime number delay

void setup() {
Serial.begin(9600);
Serial.println("Starting...");
}

void loop() {
unsigned long start = millis();
if(isPrime(count)) {
Serial.println(count);
  
if(millis() - start < printDelay) {
delay(abs(printDelay - (millis() - start)));
}
}
count++;
}

boolean isPrime(int x) {
boolean prime = true;
  
for(int i = 2; i <= x/2; i++) { //Loop every number up to half
if(x % i == 0) { //If it's divisible...
prime = false; //It isn't prime!
break;
}
}
  
return prime;
}