128k views
4 votes
Write a recursive program that tests if a number is a prime number (returns true) or not (returns false). Use a naïve (simple) algorithm that determines that a number P is not a prime number if remainder of dividing the number P by at least one number Q, Q less than P, is zero. (There exists a Q such that Q< P and P modulo Q is O) and is a prime number otherwise. Use existing Scheme pre-defined functions for checking for remainders. (is-prime-number 19) #t (is-prime-number 27) #f

1 Answer

2 votes

Answer:

int is-prime-number(int P){

if (Q == 1) {

return 1;

} else{

if (P%Q == 0){

return 0;

} else {

Q = Q - 1;

is-prime-number(P);

}

}

}

Explanation:

  • I have defined a simple function by the name of is-prime-number which takes an integer input the number to be tested whether it is prime or not and returns an integer output.
  • if the function returns 1 (which means true) this means that the number is prime otherwise the number is not prime.
  • Now we just need to call this function in our main function and print "Number is prime" if function returns one otherwise print "Number is not prime".
  • Another important point to note here is that we need to define Q as Global variable since we are not passing it as a input to the function but we are using it inside the function so it needs to be declared as a Global variable.
User Shadow Radiance
by
5.2k points