need help to calculate the factorial in ascending and descending order in an equation!
I need to solve this question: Make a program that reads the number of terms and a positive value for X. calculate and show the value from the following series: * S = (- X2/!1) (+X3/2!) (- X4 / 3!) (+X5 / 4!) (- X6 / 3!) (+X7 / 2!) (- X8 / 1!) (+X9 / 2!) (- X10 / 3!) (+X11 / 4!) -...
I have no idea how to make the factorial go up and down that way. I started by doing this Code:
for (int i = 1; i < termos; i++) {
if (expoente % 2 == 0) { //se o expoente for par, x é negativo...
x = -x;
}
fatorial = calcFat(numFat); //calcula o fatorial do numero...
expoente++;
}
So I calculate item by item, but what's missing is just doing numFat cycle from 1 to 4 and then from 4 to 1.
Ps: function created to calculate the factorial:
int calcFat(int num) { //calcula o fatorial e retorna seu valor...
int resultado = num;
for (int i = (num-1); i > 1; i--) {
resultado = resultado * i;
}
return resultado;
}
Thanks for the help!
2 answers
I managed in a not so practical but functional way and solved my problem:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
* Faça um programa que leia o número de termos e um valor positivo para X. Calcule e mostre o valor
da série a seguir:
*
S = -X2 +X3 –X4 +X5 -X6 +X7 -X8 +X9 -X10 +X11 -...
1! 2! 3! 4! 3! 2! 1! 2! 3! 4!
*/
int main(int argc, char** argv) {
int termos, x, expoente =2, numFat = 1, fatorial;
float resp;
printf("Insira o numero de termos: ");
scanf("%d", &termos);
printf("Insira o valor de x: ");
scanf("%d", &x);
for(int i = 0; i < termos; i++){
if(numFat == 4){
while(1==1){
if(expoente % 2 == 0){//se o expoente for par, x é negativo...
x = -x;
}
fatorial = calcFat(numFat);//calcula o fatorial do numero...
resp = resp +((float) powf(x, expoente) / fatorial);
numFat--;
expoente++;
if(i == termos){
break;
}
i++;
if(numFat == 1)
break;
}
}else if(numFat == 1){
while(1 ==1){
if(expoente % 2 == 0){//se o expoente for par, x é negativo...
x = -x;
}else if(expoente % 2 != 0){
x = fabs(x);
}
fatorial = calcFat(numFat);
resp = resp + ((float)powf(x, expoente) / fatorial);
numFat++;
expoente++;
if(i == termos){
break;
}
i++;
if(numFat == 4)
break;
}
}
}
printf("Valor da sequencia: %.2f", resp);
return (EXIT_SUCCESS);
}
int calcFat(int num){//calcula o fatorial e retorna seu valor...
int resultado = num;
for(int i = (num-1); i > 1; i--){
resultado = resultado * i;
}
return resultado;
}
An optimized solution, taking advantage of the details of the definition of the problem: the value of x alternates from - to +, the exponent starts with 2 and grows, the factorial tab is fixed.
#include <math.h> // pow
#include <stdio.h>
#include <stdlib.h> // abs
static int fact(int n)
{
int i, result = 1;
for (i = 1; i <= n;i++)
result *= i;
return result;
}
static int facts[6];
static void initFacts()
{
facts[0] = fact(1);
facts[1] = fact(2);
facts[2] = fact(3);
facts[3] = fact(4);
facts[4] = facts[2];
facts[5] = facts[1];
}
int main()
{
int i, nTermos, x;
double exp = 2, result = 0;
int j = 0; // para indexar facts
initFacts();
printf("*\n");
printf("* numero de termos: ");
scanf("%d", &nTermos);
printf("* valor de x: ");
scanf("%d", &x);
printf("*\n");
x = abs(x); // garante que x comeca positivo
for (i = 0; i < nTermos; i++, exp++)
{
x = -x;
result += pow(x, exp) / facts[j++];
if (j == 6)
j = 0;
}
printf("* result=%lf\n", result);
printf("*");
}