递归函数找到素数因子

时间:2010-07-10 22:59:10

标签: c recursion

我做了一个递归函数来找到一个数字的素因子,但是它有一个让turbo c退出的错误。请帮忙

#include<stdio.h>
#include<conio.h>
int prime(int num);
int primefactor(int num,int i);
void main(void)
{
    int num;
    printf("Enter a number whose prime factors are to be calculated:");
    scanf("%d",&num);
    primefactor(num,i);
     i=num 
    getch();
}
int primefactor(int num,int i)
{
    if(i==2)
    return 1;
    if(num%i==0)
    {
        if(prime(num))
        {
            printf(",%d",num);
            num=num/i;
            i++;
        }


    }
    i--;
    primefactor(num,i);
    return 0;
}
int prime(int num)
{
    int i,flag;
    for(i=2;i<num;i++)
    {
        if(num%i==0)
    flag=0;
    }
    return flag;
}

11 个答案:

答案 0 :(得分:5)

void main(void)
{
    int num,i=num; // (*)
    printf("Enter a number whose prime factors are to be calculated:");
    scanf("%d",&num);
    primefactor(num,i);
    getch();
}

您认为i(*)中会有什么价值?

不确定你想要i开头的,但我很确定你希望它是随机的。如果您希望它以num的值开头,则需要在阅读后为其分配num

void main(void)
{
    int num,i; 
    printf("Enter a number whose prime factors are to be calculated:");
    scanf("%d",&num);
    i = num; // assignment goes here.
    primefactor(num,i);
    getch();
}

答案 1 :(得分:1)

(很难写出好的代码......所以对于任何错误提前抱歉:p)

更简单的非递归版

printPrimeFactors(int num) {

  for (i = 2; i < sqrt(num); i=getNextPrime()) {
     if (num %i)
        printf("%d", i);
  } 

}

如果你必须使用递归

printPrimeFactors(int num) {

 if(isPrime(num)) {
    printf ("%d ", num);
 } else {
    for(i=2; i < sqrt(num); i++) {
        if(num%i ==0) {
              printPrimeFactors(i);
              printPrimeFactors(num/i);   
        }
    }
 }

}

答案 2 :(得分:1)

c ++中的完全递归解决方案(用c替换带有printf的cout行):

void printPrimeFactors(int num)
{
    static int divisor = 2; // 2 is the first prime number

    if ( num == 1 ) //if num = 1 we finished
    {
        divisor = 2; //restore divisor, so it'll be ready for the next run
        return;
    }
    else if ( num % divisor == 0 )  //if num divided by divisor
    {
        cout << divisor << " "; //print divisor
        printPrimeFactors( num / divisor ); //call the function with num/divisor
    }
    else //if num not divided by divisor
    {
        divisor++; //increase divisor
        printPrimeFactors( num ); 
    } 
}

答案 3 :(得分:1)

使用低开销函数调用实现素数分解的最佳方法是。 。

void factors(int number)
{
    int divisor = 2;
    if (number == 1) { cout << "1"; return; }
    while ((number % divisor) && (number > divisor)) divisor++;
    cout << divisor << ", ";
    factors(number / divisor);
}

函数调用(递归)的数量等于素因子的数量,包括1.

答案 4 :(得分:1)

我在C中这样做。根据编译器的不同,可能需要对程序进行微小的更改。

#include<stdio.h>
int primefact(int);
int main()
{
    int n;
    printf("Enter a number whose prime factors are to be calculated : \n");
    scanf_s("%d", &n);
    printf("Prime factors of %d are : ");
    primefact(n);
    printf("\n");
    return 0;
}
int primefact(int n)
{
    int i=2;
    while(n%i!=0)
        i++;
    printf("%d ", i);
    if(n==i)
        return 0;
    else
        primefact(n/i);
}

答案 5 :(得分:0)

同意IVlad - 同样,当num是素数时会发生什么?例如,将调用递归函数的次数,例如num = 7?

答案 6 :(得分:0)

#include<stdio.h>
#include<stdlib.h>

int ar[10]={0};
int i=0,j=2;

void P(int n)
{
    if(n<=1){
        return ;
    }

    else{
            if(n%j == 0){
                printf("%d\t",j);
                n=n/j;  

            }
            else{
                j++;                
            }   
        P(n);
    }
}

int main(void)
{
    int n;
    printf("Enter n = ");
    scanf("%d",&n);
    P(n);
    printf("\n");
    return 0;
}

答案 7 :(得分:0)

// recursivePrime.cpp
// Purpose: factor finding for an integer 
// Author: Ping-Sung Liao, Kaohsiung,TAIWAN
// Date: 2017/02/02
// Version : 1.0
// Reference:
// http://stackoverflow.com/questions/3221156/recursive-func-to-find-prime-factors

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


int primefactor(int num,int i);
int main(void)
{
    int num, i;
    printf("Enter a number whose prime factors are to be calculated:");
    scanf("%d",&num);
    i=int ( sqrt (num) );
    primefactor(num,i);

    system("pause");  // instead of getch()
}

int primefactor(int num,int i)
{   printf("num %d i=%d\n", num, i);
    if (i==1)
      printf("prime found= %d\n", num);  // prime appearing in he variuable num

    else if(num%i==0)
    {   primefactor( int (num/i) , int ( sqrt(num/i) ) );
        primefactor( i , int (sqrt ( i ) ) );       
    }
    else 
    {  i--;
       primefactor(num,i);
    }
    return 0;
}

答案 8 :(得分:0)

#include  <`stdio.h`>

void pf(int,int);

int main()

{

int a,i=2;

     printf("Enter the Number:\n");
     scanf("%d",&a);

     pf(a,i);
}

void pf(int x,int y)

{

   if(x==1)

   return 1;

    else
    {
       if(x%y==0)
       {printf("%d\t",y);
        pf(x/y,y);
       }

       else
       {
        y++;
        pf(x,y);
       }
    }
}

答案 9 :(得分:0)

这是一个古老的问题,但是我仍然想提供我的答案。 [不要不尝试我的代码就投下反对票。有用! ]

我们不需要编写函数来计算下一个质数。例如,如果num是24,并且我们将其连续除以2,直到不再被2整除,那么2的其他倍数也不能除以该数字。因此最终,只有(可能)质数可以完美地除以任何正整数。

这是我的代码:(我已经为迭代和递归逻辑编写了源代码)

#include<stdio.h>  

void pfactors_rec(int, int);  
void pfactors(int);  

int main()  
{  
    int num;  

    printf("Enter a positive integer number\n");  
    scanf("%d", &num);  

    printf("\nPrime Factors of %d without using recursion\n", num);  
    pfactors(num);  

    printf("\nPrime Factors of %d using recursion\n", num);  
    pfactors_rec(num, 2);  

    return 0;  
}  

void pfactors_rec(int num, int count)  
{  
    if(num < 1)  
        return;  
    else if(num % count == 0)  
    {  
      printf("%d\n", count);  
      pfactors_rec(num / count, count);  
    }  
    else  
    {  
      pfactors_rec(num, count+1);  
    }  
}  

void pfactors(int num)  
{  
    int count;  

    for(count = 2; (num > 1); count++)  
    {  
        while(num % count == 0)  
        {  
            printf("%d\n", count);  
            num = num / count;  
        }  
    }  
    printf("\n");  
}  

您可以在C Program To Find Prime Factors of a Number using Recursion上找到视频讲座和完整的注释。

答案 10 :(得分:-1)

在java中实现。

public class PrimeFactor {

public int divisor=2;
void printPrimeFactors(int num)
{

    if(num == 1)
        return;

    if(num%divisor!=0)
        {
        while(num%divisor!=0)
            ++divisor;          
        }
    if(num%divisor==0){

    System.out.println(divisor);
        printPrimeFactors(num/divisor);
    }

}
public static void main(String[] args)
{
    PrimeFactor obj = new PrimeFactor();
    obj.printPrimeFactors(90);
}

}