C Recursive Function for prime number with just one parameter
40,505
Solution 1
You can use global variable
#include<stdio.h>
int isPrime(int);
int globalChk; //Global Variable
int main(){
int num=73;
int prime;
globalChk = num/2;
prime = isPrime(num);
if(prime==1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
int isPrime(int num){
if(globalChk==1){
return 1;
}else{
if(num%globalChk==0) {
return 0;
} else {
globalChk = globalChk-1;
isPrime(num);
}
}
}
Solution 2
float isPrime(float n) {
if ((int)n == 2)
return 1;
if ((int)(1/(n - (int)n)) % (int)n == 0)
return 0;
if (n / (int)n == 1)
return isPrime(n-1 + 1/n); // store original parameter to decimal places
return isPrime(n-1);
}
Usage:
if ((int)isPrime(n) == 1) {
// do this if n is a prime number
}
Solution 3
#include <stdio.h>
#include <math.h>
int isItPrime(double);
int main()
{
printf("Primes from 0 to 100:");
for (int n = 1; n < 100; n++)
{
if (isItPrime(n) == 1) //if it's a prime
printf(" %d", n);
}
return 0;
}
int isItPrime(double n) {
if(n<=1)
return 0;
long rounded = (long)n;//state of the iteration
double decimal = n - rounded;
if (decimal == 0){ //start condition for integers
return isItPrime( (double)(2.0 + 1.0/n)); //start with 2 then go all the way to sqrt(number being checked)
}
else {
long checkNum = round(1.0/decimal); //integer that is being checked for primality
if (checkNum == 2)
return 1;
long root = (long)(sqrt(checkNum));//square root of number being checked
if( rounded >= root) //exit condition
return !(checkNum % rounded == 0); //return prime if not a perfect square,
else{
if(checkNum % rounded == 0)
return 0;
else
return isItPrime((double)rounded + 1 + decimal); //recursive method call is here
}
}
}
Output: Primes from 0 to 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Author by
Rui Moreira
Updated on June 01, 2021Comments
-
Rui Moreira almost 3 years
Im trying to make a function to check if a number is prime number or not, using recursion. The best two examples are these two programs (one without recursion, one using recursion).
Using recursion:
#include<stdio.h> int isPrime(int,int); int main(){ int num,prime; printf("Enter a positive number: "); scanf("%d",&num); prime = isPrime(num,num/2); if(prime==1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); return 0; } int isPrime(int num,int i){ if(i==1){ return 1; }else{ if(num%i==0) return 0; else isPrime(num,i-1); } }
Without using recursion:
#include<stdio.h> int isPrime(int); int main(){ int num,prime; printf("Enter a positive number: "); scanf("%d",&num); prime = isPrime(num); if(prime==1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); return 0; } int isPrime(int num){ int i=2; while(i<=num/2){ if(num%i==0) return 0; else i++; } return 1; }
My question is: Is it possible to make a function to check if a number is prime number or not, using recursion, but with just one parameter (like this: int isPrime(int num))?
Any help is appreciated