Benjamin John
Benjamin John

Reputation: 69

Prime Factors displayed in multiplication format

This is for an assignment so I'll just post the question details with my code right below it so you guys can follow easier than if I were to explain the problem myself.

Q) Any integer above 1 is either a prime number, or can be made by multiplying prime numbers together. For example: 16 = 2 x 2 x 2 x 2 = (2^4) 17 = (17^1) 18 = 2 x 3 x 3 = (2^1) x (3^2) Write a C program, primeFactor.c, that asks the user for an integer number greater than 1 and displays the product of its prime factors.

A Sample interaction is as follow:

Enter a valid number (>1): 18

18 = 1 x (2^1) x (3^3)

Enter a valid number (>1): 0

Invalid number.

Enter a valid number (>1): 19

19 = 1 x (19^1)

int readNum(int num)
{
    if (num <= 1)
    {
        return printf("Invalid Number.");
    }
    else
    {
        return printf("%i = ", num);
    }
}

int pcount = 0;
bool isPrime(int prime)
{
    bool p_flag = false;

    for(int i = 2; i < prime; i++)
    {
        if(prime % i == 0)
        {
            pcount += 1;
        }
    }
    if (pcount > 0)
    {
        p_flag = false;
    }
    else if (pcount == 0)
    {
        p_flag = true;
    }

    return p_flag;
}

int findPrimeCount(int num, int prime)
{
    int freq = 1;

    printf("1 ");
    for(prime = 2; prime < num; prime++)
    {
        freq = 1;
        while((num % prime) == 0)
        {
            printf(" * (%i ^ %i)", prime, freq);
            num = num / prime;
            freq++;
        }
    }

    if(num != 1)
    {
        printf(" * (%i ^ 1)", num);
    }

    return num;
}

int main(void)
{
    int number = 0;

    printf("Enter a valid number (>1): ");
    scanf("%i", &number);

    readNum(number);

    if(isPrime(number) == 1)
    {
        printf("1 * (%i ^ 1)", number);
    }
    else if (isPrime(number) == 0)
    {
        findPrimeCount(number, 2);
    }

    return 0;
}

Right now I guess I'm stuck in that if I input the number 64 the output will be:

64 = 1 * (2^1) * (2^2) * (2^3) * (2^4) * (2^5) * (2^6)

where I want the output to be just:

64 = 1 * (2^6)

I'm not sure how I would go about truncating the string outputs before that or possibly even avoiding printing that whole thing out altogether. Any help is much appreciated!

Upvotes: 2

Views: 507

Answers (1)

StaticBeagle
StaticBeagle

Reputation: 5175

In your findPrimeCount function, just put the print statement outside of your while loop so it only prints the factor^highestFreq:

int findPrimeCount(int num, int prime)
{
    int freq = 1;

    printf("1 ");
    for(prime = 2; prime < num; prime++)
    {
        freq = 1;
        while((num % prime) == 0)
        {

            num = num / prime;
            freq++;
        }
        // only print the last iteration
        // subtract 1 to frequency to equalize
        // the last freq++.
        freq--;
        // filter out num^0
        if(freq > 0)
        {
            printf(" * (%i ^ %i)", prime, freq);
        }
    }

    if(num != 1)
    {
        printf(" * (%i ^ 1)", num);
    }

    return num;
}

For input = 64 outputs:

64 = 1  * (2 ^ 6)

input = 46 outputs:

46 = 1  * (2 ^ 1) * (23 ^ 1)

Upvotes: 2

Related Questions