0

我目前是一名学生,试图将阶乘打印为素数乘以某些指数,如下所示:

5!= (2^3) (3^1) (5^1)

但是,我不断收到一个不寻常的错误,该错误发生在使用 scanf 检索我的输入之后(顺便说一下,我非常感谢有人向我展示如何从外部文件中检索多个输入以使用输入重定向来执行此操作,因为这就是我们应该为此检索我们的输入)。

无论如何,我假设这个错误在我的 while 循环规范中的某个地方。我将不胜感激任何帮助/提示/指针。谢谢!

#include <stdio.h> //headers
#include <stdbool.h>

//function prototypes - I will be using functions inside of each other 

int find_prime_count (int prime, int num); 
int find_next_prime (int prime); 
bool is_prime (int num);

int main(void) //main function 
{
    int primeCount[100] = {0}, prime = 2, fact, i = 2, temp = 2, currentPrimeCount, printCount = 0;  

    printf ("Enter number: "); 
    scanf ("%d", &fact);


    while (i <= fact)
    {
        printf ("i is less than factorial");
        while (temp != 1)
        {
            printf ("Temp is not equal to one");
            currentPrimeCount = find_prime_count (prime, temp); 
            printf ("currentPrimeCount calculated");
            temp = temp / (currentPrimeCount * prime);
            printf ("Temp updated");
            primeCount[prime + 1] += currentPrimeCount; 
            printf ("primeCount[prime + 1] updated");
            prime = find_next_prime (prime); 
            printf ("Next prime found");
        }

        i += 1; 
        temp = i; 
    }

    printf ("%3d! =  ", fact); 
    i = 0; 
    while (i < 100)
    {
        if (primeCount[i] != 0)
        {
            if (printCount == 0)
            {
                printf ("(%d^%d)", i, primeCount[i]); 
            }

            else if (printCount != 0)
            {
                printf (" * (%d^%d)", i, primeCount[i]); 
            }

            printCount += 1; 

            if ((printCount % 9) == 0)
            {
                printf ("/n");
            }

            if ((printCount > 9) && ((printCount % 9) == 0))
            {
                printf ("       ");
            }

        }

    }

    return 0;
}



bool is_prime (int num)
{
    bool check = true; //sets check variable to true
    int i = 2; //starts counter variable at 2 (will test all numbers >=2 && <num)

    while (i < num && check == true)
    {
        if ((num % i) == 0) //if it is divisible by any number other than 1 and itself
        {
            check = false; //it is not a prime number and the check becomes false
        }

        i += 1; //increasing counter 
    }

    return check; //returns boolean value
}

int find_next_prime (int prime)
{
    int i = prime; 
    bool check = false; 
    printf ("find_next_prime starts.");

    while (check == false)
    {
        i += 1;
        check = is_prime (i); 
    }

    printf ("find_next_prime ends.");

    return i; 
}

int find_prime_count (int prime, int num)
{
    int count = 0; 
    printf ("find_prime_count starts.");

        while ((prime % num) == 0)
        {
            count += 1; 
            num = num / prime; 
        }

    printf ("find_prime_count ends.");

    return count; 
}
4

1 回答 1

2

使用 gdb,我可以看出这是prim % num.

提示:

  1. -g使用标志编译
  2. 运行使用gdb
  3. 设置断点...
于 2017-01-29T05:10:03.647 回答