Factorial calculation in an arithmetic progression

Asked

Viewed 126 times

0

I need to create a program that calculates the factorial started with 2 and add 2 more to each repetition until it reaches 20 times.

Ex: 2!, 4!, 6!, 8!...

I tried several ways, but every time I compile it gives me the first answer of the correct factored number, but the next ones are all incorrect.

Código compilado

#include<stdio.h>

int main(){
    int x = 0;
    int i = 1;
    int fat;


    while(i <= 20){

        x = x + 2;

        for(fat = 1; x > 1; x = x - 1){
            fat = fat * x;
        }


        printf("%d\n", fat);
        i = i + 1;
    }
}

The expected result would be 2, 24, 720, 40320, but this giving 2, 6, 6, 6...

How to fix this?

  • First you need to find a criterion. The progression clearly shows it is not arithmetic. Having a clear definition of what needs to be done gives to look at the code, which is confusing.

  • I suggest you do the table test to understand what is happening. At the end of the for the value of x will be 1, and in the next iteration of while, it will start at 3 (i.e., you are calculating the factorial 3 several times). It’s also unclear if 20! is the highest value to be calculated, or if there are 20 terms (which would end the sequence at 40! , a number with more than 47 digits and which causes overflow in the results)

  • Note that you modify the value of x within your command for. When exiting the loop the variable x will always be worth 1.

  • A hint: you don’t need this for. If the idea is to show 2!, then 4!, 6! and so on, you don’t have to calculate everything from the beginning. If you have already calculated 2!, just multiply by 3 and 4 to get 4!, then just multiply by 5 and 6 to get 6! and so on

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

2 answers

1

The logic is very complicated, let’s think about it a little bit. You have terms that start at 2 and evolve from 2 to 2. So in the loop let’s call this termo instead of i as called to give more semantics to what we are doing since the loop is part of the problem domain is not just a mechanism to scan a data collection.

Let’s go two for two on one for which is simpler than a while.

Let’s start the factorial where it always starts which is 1. And we will apply the calculation of it always using two steps (you can do in one but complicates the understanding).

Before finding the factorial of the current term we need to find the factorial of the previous term because to show jumps of 2 in 2, but to calculate no, has to calculate all factorial, after all the factorial is always based on its previous factorial, can not skip one of the numbers. Then I count with the previous term and soon after you find that I do with the current one. And that’s it, I have the necessary factorial.

As I am accumulating I don’t need to calculate all factorial again, I already have the value of the previous term, so just repeat this step. So:

#include<stdio.h>

int main() {
    int fat = 1;
    for (int termo = 2; termo <= 20; termo += 2) {
        fat *= termo - 1;
        fat *= termo;
        printf("%d\n", fat);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I’m not sure if it should end in 20, but if it’s 20 terms jumping from 2 to 2, and not just until the 20th. term, just make the loop go up 40.

0

Thank you guys, I managed to find a way to resolve thanks to the comments and reply.

I did it this way:

#include<stdio.h>

int main(){
    int x = 2;
    int i = 1;
    double fat;
    int aumento1 = 3, aumento2 = 4;

    for(fat = 1; x > 1; x = x - 1){
        fat = fat * x;
    }

    while(i <= 20){

        printf("%lf\n", fat);
        fat = fat * aumento1 * aumento2;
        aumento1 = aumento1 + 2;
        aumento2 = aumento2 + 2;

        i = i + 1;
    }
}

I used two variables to add +2 to each repetition that I used to calculate the factorial, being this case:

        fat = fat * aumento1 * aumento2;
        aumento1 = aumento1 + 2;
        aumento2 = aumento2 + 2;

Once again thank you, you’ve helped me a lot!

I’m not used to it here so I don’t know how to close, or if there’s any way I can close an argument.

  • It can be complicated to simplify!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.