C递归程序,用于打印给定数字的所有素因子,从最大因子到最小因子

时间:2017-06-10 13:40:45

标签: c recursion prime-factoring

我试图编写一个程序来打印给定数字的素因子,我需要将它们从最大因子打印到最小因子,例如: 对于输入 180 ,输出将为: 5 * 3 * 3 * 2 * 2 , 有什么建议?这就是我现在所得到的:

#include<stdio.h>


void print_fact(int n)
{
    if (n==1)
        return;
    int num=2;
    while (n%num != 0)
        num++;
    printf("*%d",num);
    print_fact (n/num);

}

int main ()
{
    int n;
    printf("please insert a number \n");
    scanf("%d",&n);
    print_fact(n);
}

对于此代码,输出为:

*2*2*3*3*5

4 个答案:

答案 0 :(得分:3)

您可以在递归调用返回后简单地打印输出。您需要稍微修改我显示*的方式,我留给您。

#include<stdio.h>


void print_fact(int n)
{
    if (n==1)
        return;
    int num=2;
    while (n%num != 0)
        num++;
     // printf("*%d",num); // remove from here
    print_fact (n/num);
    printf("%d ",num); // put here

}

int main ()
{
    int n;
    printf("please insert a number \n");
    scanf("%d",&n);
    print_fact(n);
}

输入180的输出是:

5 3 3 2 2 

除此之外,还有更有效的方式来实际找到数字。

答案 1 :(得分:1)

从数学角度讲,按升序顺序找到它们要快得多。更快,更快。

如果你不想用动态数组来打扰自己,解决方法就是递归。找到最低素数因子,递归分出的数字(num /= fac),然后然后打印先前找到的因子,这将显示 last

答案 2 :(得分:0)

要更改它们的打印顺序,您可以在printf语句后放置print_fact语句。要摆脱领先*,您可能希望存储结果并在计算后显示它们

答案 3 :(得分:0)

好吧,我正在尝试优化我的算法 这是我现在的代码: 功能代码

#include "prime_func.h"


int divisors(int x) /* Function To set the maximum size of the future array,
                       Since there is no way to find the number of primary factors 
                       without decomposing it into factors,
                       we will take the number of total number divisors 
                       (which can not be greater than the number of primary factors) */
{
    int limit = x;
    int numberOfDivisors = 0;

    if (x == 1) return 1;

    for (int i = 1; i < limit; ++i) {
        if (x % i == 0) {
            limit = x / i;
            if (limit != i) {
                numberOfDivisors++;
            }
            numberOfDivisors++;
        }
    }

    return numberOfDivisors;
}
void find_fact(int n, int *arr, int size, int i) //func to find the factors and apply them in allocated array
{
    int num = 2;
    if (n < 2) 
    {
        printf("error\n");
        return;
    }
    while (n%num != 0)
        num++;
    arr[i++] = num;
    find_fact(n / num, arr, size, i);


}
void print_fact(int *arr, int size) // func to print the array in reverse
{
    int i = 0;
    int first;
    first = FirstNumToPrint(arr, size);
    for (i = first; i>0; i--)
        printf("%d*", arr[i]);
    printf("%d", arr[0]);
}

int FirstNumToPrint(int *arr, int size) // func to find the first number to print (largest prime factor)
{
    int i;
    for (i = 0; i < size; i++)
        if (arr[i] == 0)
            return i - 1;
}


int first_prime(int num) // for now i'm not using this func
{
    for (int i = 2; i<sqrt(num); i++)
    {
        if (num%i == 0)
        {
            if (isprime(i));
            return(i);
        }
    }
}
bool isprime(int prime) // for now i'm not using this func
{
    for (int i = 2; i<sqrt(prime); i++)
    {
        if (prime%i == 0)
            return(false);
    }
    return(true);
}

主要代码

#include "prime_func.h"

int main()
{
    int n,i=0; // first var for input, seconde for index
    int *arr; // array for saving the factors
    int size;//size of the array
    printf("please insert a number \n");// asking the user for input
    scanf("%d", &n);
    size = divisors(n); //set the max size
    arr = (int *)calloc(size,sizeof(int)); //allocate the array
    if (arr == NULL) // if the allocation failed
    {
        printf("error\n");
        return 0;
    }
    find_fact(n, arr,size,i);// call the func
    print_fact(arr,size); //print the result
    free(arr); // free memo
}

@WillNess @GoodDeeds @mcslane