Viết các hàm đệ quy để tính: S1 = 1+2 +3+......+n ; S2 = 1+1/2 + .....+ 1/n ; S3 = 1-1/2 +......+ (-1)n+1 1/n S4 = 1 + sin(x) + sin2(x) + ......+ sinn (
1 câu trả lời
uses crt, math;
var x, n: longint;
function calc_S1(n: longint): longint;
begin
if n < 1 then exit(0);
exit(n + calc_S1(n - 1));
end;
function calc_S2(n: longint): real;
begin
if n < 1 then exit(0);
exit(1/n + calc_S2(n - 1));
end;
function calc_S3(n: longint): real;
begin
if n < 1 then exit(0);
if n mod 2 = 0 then exit(-1/n + calc_S3(n - 1)) else exit(1/n + calc_S3(n - 1));
end;
function pow(a: real; n: longint): real;
var tmp: real;
begin
if n = 0 then exit(1);
tmp:=pow(a, n div 2);
if n mod 2 = 0 then exit(tmp * tmp);
exit(tmp * tmp * a);
end;
function calc_S4(a, n: longint): real;
var tmp: real;
begin
if n < 0 then exit(0);
exit(pow(sin(a), n) + calc_S4(a, n - 1));
end;
begin
readln(x, n);
writeln(calc_S1(n));
writeln(calc_S2(n):0:3);
writeln(calc_S3(n):0:3);
writeln(calc_S4(x, n):0:3);
readln;
end.