Well, we have three variables in this context: y
is the value to be discovered, i
is probably an informed value to do the calculation and n
would be the number of times the calculation should be done. Home
Analyzing the problem:
y = i - i^2 + i^3 -i^4 +i^5 - ... +- i^n
I noticed a pattern. Initially we have the value of i
, and then a subtraction by the sum of i
raised to the successor of each power (i^2) + (i^3)
and soon after that pair is subtracted by the sum of another pair following the same pattern: ((i^2) + (i^3)) - ((i^4) + (i^5))
.
Home
If I have intended correctly, the following code in C # would theoretically solve the problem:
Console.WriteLine("Informe o valor de i: ");
double i = double.Parse(Console.ReadLine());
Console.WriteLine("Informe o valor de n: ");
int n = int.Parse(Console.ReadLine());
double y = i;
for(int x = 2; x <= n; x++)
{
y = y - (Math.Pow(i,x) + Math.Pow(i,(x+1)));
x++;
}
Console.WriteLine("O valor de y = " + y);
I did the following Fiddle where you can do tests ...
As a task, just try to run C ++.
If I do not get it right, please just comment first
of -1.