Home > Software design >  Certain values give incorrect results for prime number checking function
Certain values give incorrect results for prime number checking function

Time:10-10

I recently wrote a program in C for a calculator. To produce a function that checks if the user input is a prime number or not (amongst other functions).

I essentially used this code (excluding all other functions):

#include <stdio.h>
#include <math.h>

int testForPrime(int);

int main(void) {

    int ioperand1 = 0;
    printf("\nEnter the value to check if prime (positive integer): ");
    scanf("%d", &ioperand1);
    if (testForPrime(ioperand1) != 0)
        printf("\nThis number is prime.\n");
    else
        printf("\nThis number is not prime.\n");
    return 0;
}

int testForPrime(int operand1) {

    int i = 0;
    for (i = 2; i <= sqrt(operand1); i  ) {
        if (operand1 == 0 || operand1 == 1)
            return 0;
        else if (operand1 % i == 0) 
            return 0;
        else
            return 1; 
    }
}
  

^ This code above produces the errors

I am not sure why the code produces an error for the value 9 (I fixed that above by adding the condition: if (operand1 == 9), but I don't understand why 9 is seemingly the only value that results in an incorrect solution (It would say 9 was prime, but not any other number give an incorrect result).

One other bug that I remidied with an extra condition statement was the value of 2. Before adding the extra conditional statement in the main function: if (ioperand1 == 2), the value 2 would always come up as a non prime number. I originally found this solution to check for prime numbers online, and I still don't understand why the for loop starts from 2.

#include <stdio.h>
#include <math.h>

int testForPrime(int);

int main(void) {

    int ioperand1 = 0;
    printf("\nEnter the value to check if prime (positive integer): ");
    scanf("%d", &ioperand1);
    if (testForPrime(ioperand1) != 0 || ioperand1 == 2)
        printf("\nThis number is prime.\n");
    else
        printf("\nThis number is not prime.\n");
    return 0;
}

int testForPrime(int operand1) {

    int i = 0;
    for (i = 2; i <= sqrt(operand1); i  ) {
        if (operand1 == 0 || operand1 == 1 || operand1 == 9)
            return 0;
        else if (operand1 % i == 0) 
            return 0;
        else
            return 1; 
    }
}

^This code above fixed the problem, though I don't undesttand why the problem existed in the first place.

TL;DR: I don't know why this code doesn't work without the extra conditional statements: if (operand1 == 9) in function definition, and if (ioperand1 == 2) in main function.

If anyone could help clear this up, I'd appreciate it.

CodePudding user response:

It is because your prime checking loop does not iterate. It always returns on the first iteration. It must run to completion, and then the number will be prime. So

int testForPrime(int operand1) {
    if(operand1 < 2) {
        return 0;
    }
    int sr = (int)round(sqrt(operand1));
    for(int i = 2; i <= sr; i  ) {
        if (operand1 % i == 0) {
            return 0;
        }
    }
    return 1;
}
  •  Tags:  
  • c
  • Related