C语言 仅一个参数的质数递归函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26807173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:32:53  来源:igfitidea点击:

C Recursive Function for prime number with just one parameter

cfunctionrecursionparameters

提问by Rui Moreira

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))?

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

Any help is appreciated

任何帮助表示赞赏

回答by α?jiβ

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

回答by Simon Pham

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
}

回答by ray lin

#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

输出:从 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

回答by Manjunath N

Declare the variable num variable before main() function and pass the `num/2` to the isprime() function.

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