I need to calculate the sum of the number a (double) to the power of n (int). Without pow function !!!!!
I will be very grateful.
I have done this way, but it is very complex, I want something simpler.
If you know how to make this via Java or C++ or Pascal please reply too)
1/a2 + 1/a4 + 1/a6 … + 1/a2n.
var i, n: integer;
s, a, x: real;
f: boolean;
begin
repeat
write('n = '); readln(n);
if n < 1 then writeln('Error: n <= 0, reenter.')
until n >= 1;
repeat
write('a = '); readln(a);
if a = 0 then writeln('Error: a = 0, reenter.')
until a <> 0;
s := 0;
x := 1;
f := true;
for i := 1 to n do
begin
x := x / a / a;
if x = 0
then begin
writeln('Float rounding error.');
f := false;
break
end;
s := s + x
end;
if f then writeln('s = ', s);
readln
end.```
>Solution :
In C:
double bPow = 1;
double sum = 0
double b = a * a;
for (int i = 0; i < n; i++)
{
bPow *= b;
sum += 1 / bPow;
}
This gives you the sum of all 1/a^x for x = [2, 4, …, 2n] .