The classic problem still eludes me ... I do not know why this program is looping through up the divisor variable without stopping and going to the final "else if" ... it will identify a non prime number fine but then it loops through all of the divisors or a prime number until it becomes equal to itself and it declares it self not prime.
float number = 101.0;
float place_holder = 0;
boolean is_prime = true;
float divisor = 3.0;
//int test_number = int(ceil(sqrt(number)));
void setup() {
if (number == 0) {
println(number + " is undefined");
} else if (number > 0 && number <= 3) {
println(number + " is prime");
} else if (number % 2 == 0 && number > 2) {
println(number + " is not prime") ;
} else if (number > 2 && number % 2 !=0) {
while ( is_prime == true) {
if (number % divisor == 0) {
is_prime = false;
divisor = 3;
println(number+ " is not prime");
number = number +1;
} else if ( number % divisor !=0) { // problem line :: I dont know why it always loops HERE
divisor = divisor + 1;
println(number + "number" + divisor + "divisor");
} else if ( number -1 == divisor) {
println(number + " is prime");
is_prime = true;
}
}
}
}
float number = 101.0;
float place_holder = 0;
boolean is_prime = true;
float divisor = 3.0;
//int test_number = int(ceil(sqrt(number)));
void setup() {
if (number == 0) {
println(number + " is undefined");
} else if (number > 0 && number <= 3) {
println(number + " is prime");
} else if (number % 2 == 0 && number > 2) {
println(number + " is not prime") ;
} else if (number > 2 && number % 2 !=0) {
while ( is_prime == true) {
if (number % divisor == 0) {
is_prime = false;
divisor = 3;
println(number+ " is not prime");
number = number +1;
} else if ( number % divisor !=0) { // problem line :: I dont know why it always loops HERE
divisor = divisor + 1;
println(number + "number" + divisor + "divisor");
} else if ( number -1 == divisor) {
println(number + " is prime");
is_prime = true;
}
}
}
}
1