C递归函数只有一个参数的素数

时间:2014-11-07 17:55:22

标签: c function recursion parameters

我试图使用递归来创建函数以检查数字是否为素数。最好的两个例子是这两个程序(一个没有递归,一个使用递归)。

使用递归:

    #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);
    }
}

不使用递归:

     #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;
}

我的问题是:是否可以使用递归来创建函数来检查数字是否为素数,但只使用一个参数(如:int isPrime(int num))?

感谢任何帮助

5 个答案:

答案 0 :(得分:3)

您可以使用全局变量

#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);
    }
  }
}

答案 1 :(得分:1)

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);
}

用法:

if ((int)isPrime(n) == 1) {
    // do this if n is a prime number
}

答案 2 :(得分:1)

#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
        }
    }
}

输出: 素数从0到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

答案 3 :(得分:0)

在main()函数之前声明变量num变量,并将`num / 2`传递给isprime()函数。

答案 4 :(得分:0)

使用支持函数。 像这样:

#include <iostream>

using namespace std;

bool numPrimRic(int num,int div){
    if(div>num/2) return true;
    if(num%div==0) return false;
    return numPrimRic(num,div+2);
}

bool initPrimo(int num){
    if(num%2 == 0) return false;
    return numPrimRic(num,3);
}

int main()
{
    setlocale(LC_ALL,"italian");
    int numero;
    cout << "Enter a number:" << endl;
    cin>>numero;
    if(initPrimo(numero))
        cout<<"The number is prime";
    else
        cout<<"the number isn't prime";
    return 0;
}