2018年2月1日 星期四

Delphi - 使用迴圈與遞迴計算數學題

(1) 1+2+3+4+5+...,
    當項數(n) = 50,值為多少
(2) 1+3+5+7+9...,
    當項數(n) = 50,值為多少
(3) 2+4+6+8+10+...,
    當項數(n) = 50,值為多少
(4) 1+1/(1+2)+1/(1+2+3)+...+1/(1+2+3+...),
    當項數(n) = 50,值為多少
(5) 1-2+3-4+5-6+7...,
    當項數(n) = 50,值為多少

(6) 1+1/2+1/3+1/4+1/5+1/6+1/7......
     當項數(n) = 10 值為多少? .     
(7) 1-1/2+1/3-1/4+1/5-1/6+1/7-1/8......
    當項數(n) = 10 值為多少?
(8) 1+2!+3!+4!+...+n!...
    當項數(n) = 10 值為多少?
(9) (1)+(1+1/2)+(1+1/2+1/3)+(1+1/2+1/3+1/4)+(1+1/2+1/3+1/4+1/5)......
    當項數(n) = 10 值為多少?
(10) 1+1/2+2/3+3/4+4/5...+(n-1)/n
    當項數(n) = 10 值為多少?

var n : integer;     //項數;

//----------------------------------------------------------
//(1) 1+2+3+4+5+..., 當項數(n) = 20,值為多少

function recurs1(n:integer):integer;
var ans : integer;
begin
  if n > 1 then
    ans:=n+recurs1(n-1)
  else
    ans:=1;
  result:=ans;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
    sum : integer;
begin
  n := strtoint(edit1.Text) ;
  sum := 0;

  //方法1 迴圈
  for i := 1 to n do
    sum := sum+(i);
  Math01.Caption:=sum.ToString;

  //方法2 遞迴
  sum := recurs1(n);
  showmessage(sum.ToString);
end;

//----------------------------------------------------------
//(2) 1+3+5+7+9..., 當項數(n) = 20,值為多少

function recurs2(n:integer):integer;
var ans : integer;
begin
  if n > 1 then
    ans := (2*n-1) + recurs2(n-1)
  else
    ans := 1;
  result:=ans;
end;

procedure TForm1.Button2Click(Sender: TObject);
var i : integer;
    sum : integer;
begin
  n := strtoint(edit1.Text) ;
  sum := 0;

  //方法1 迴圈
  for i := 1 to n do
  begin
    sum := sum + (2*i-1);
  end;
  Math02.Caption:=sum.ToString;

  //方法2 遞迴
  sum := recurs2(n);
  showmessage(sum.ToString);
end;

//----------------------------------------------------------
//(3) 2+4+6+8+10+..., 當項數(n) = 20,值為多少

function recurs3(n:integer):integer;
var ans : integer;
begin
  if n>1 then
    ans := (2*n) + recurs3(n-1)
  else
    ans:=2;
  result:=ans;
end;

procedure TForm1.Button3Click(Sender: TObject);
var i:integer;
    sum:double;
begin
  n := strtoint(edit1.Text) ;
  sum := 0;

  //方法1 迴圈
  for i:=1 to n do
  begin
    sum:=sum + (2*i);
  end;
  Math03.Caption := sum.ToString;

  //方法2 遞迴
  sum := recurs3(n);
  showmessage(sum.ToString);
end;

//----------------------------------------------------------
//(4) 1+1/(1+2)+1/(1+2+3)+...+1/(1+2+3+...), 當項數(n) = 20,值為多少

function recurs4(n:integer):double;
var ans:double;
    denominator:integer; //分母
begin
  if n>1 then
  begin
    denominator := recurs1(n);
    ans := 1/denominator + recurs4(n-1)  ;
  end
  else
    ans:=1;
  result:=ans;
end;

procedure TForm1.Button4Click(Sender: TObject);
var i,j,denominator:integer;
    sum:double;
begin
  n := strtoint(edit1.Text) ;
  sum := 0;

  //方法1 迴圈
  for i:=1 to n do
  begin
    denominator := 0; //分母
    for j:=1 to i do
      denominator := denominator +  j;
    sum:=sum + 1/denominator;
  end;
  Math04.Caption := format('%.7f',[sum]);

  //方法2 遞迴
  sum := recurs4(n);
  showmessage(format('%.7f',[sum]));
end;

//----------------------------------------------------------
//(5) 1-2+3-4+5-6+7..., 當項數(n) = 20,值為多少

function recurs5(n:integer):integer;
var addition,ans : integer;
begin

  if n > 1 then
  begin
    if (n mod 2)=1 then
      addition := n
    else
      addition := -n;

    ans:= addition + recurs5(n-1);
  end
  else
    ans:=1;
  result:=ans;
end;

procedure TForm1.Button5Click(Sender: TObject);
var i,symbol:integer;
    sum : integer;
begin
  n := strtoint(edit1.Text) ;
  sum := 0;

  //方法1 迴圈
  for i:=1 to n do
  begin
    if (i mod 2)=1 then
      symbol := 1
    else
      symbol := -1;
    sum := sum + symbol * i;
  end;
  Math05.Caption := sum.ToString;

  //方法2 遞迴
  sum := recurs5(n);
  showmessage(sum.ToString);
end;

//----------------------------------------------------------
//(6) 1+1/2+1/3+1/4+1/5+..., 當項數(n) = 20 值為多少?    

function recurs6(n:integer):double;
var ans : double;
begin
  if n>1 then
    ans := 1/n+recurs6(n-1)
  else
    ans := 1;
  result:=ans;
end;

procedure TForm1.Button6Click(Sender: TObject);
var i:integer;
    sum : double;
begin
  n := strtoint(edit1.Text) ;
  sum := 0;

  //方法1 迴圈
  for i:=1 to n do
  begin
    sum := sum + 1/i;
  end;
  Math06.Caption := format('%.7f',[sum]);

  //方法2 遞迴
  sum := recurs6(n);
  showmessage(format('%.7f',[sum]));
end;

//----------------------------------------------------------
//(7) 1-1/2+1/3-1/4+1/5-1/6+1/7-1/8......, 當項數(n) = 20 值為多少?

function recurs7(n:integer):double;
var ans:double;
    symbol:integer;
begin
  if n>1 then
  begin
    if (n mod 2)=0 then
      symbol := -1
    else
      symbol := 1;
    ans := symbol * 1/n + recurs7(n-1);
  end
  else
    ans := 1;
  result:=ans;
end;

procedure TForm1.Button7Click(Sender: TObject);
var i,symbol:integer;
    sum : double;
begin
  n := strtoint(edit1.Text) ;
  sum := 0;

  //方法1 迴圈
  for i:=1 to n do
  begin
    if (i mod 2)=0 then
      symbol := -1
    else
      symbol := 1;
    sum := sum + symbol * 1/i;
  end;
  Math07.Caption := format('%.7f',[sum]);

  //方法2 遞迴
  sum := recurs7(n);
  showmessage(format('%.7f',[sum]));
end;

//----------------------------------------------------------
//(8) 1+2!+3!+4!+...+n!..., 當項數(n) = 20 值為多少?

Function recurs8(n:integer):integer;
var i,addition,ans : integer;
begin
  addition := 1;

  if n>1 then
  begin
    for i := 1 to n do
      addition := addition * i;
    ans := addition + recurs8(n-1);
  end
  else
    ans:=1;
  result:=ans;
end;

procedure TForm1.Button8Click(Sender: TObject);
var i,j,addition,sum:integer;
begin
  n:=strtoint(edit1.Text) ;
  sum:=0;

  //方法1 迴圈
  for i:=1 to n do
  begin
    addition := 1;

    for j:=1 to i do
      addition := addition * j;
    sum := sum + addition;
  end;
  Math08.Caption := sum.ToString;

  //方法2 遞迴
  sum := recurs8(n);
  showmessage(sum.ToString);
end;

//----------------------------------------------------------
//(9) (1)+(1+1/2)+(1+1/2+1/3)+(1+1/2+1/3+1/4)+... , 當項數(n) = 20 值為多少?

function recurs9(n:integer):double;
var ans : double;
begin
  if n > 1 then
  begin
    ans := recurs6(n) + recurs9(n-1);
  end
  else
    ans:=1;
  result:=ans;
end;

procedure TForm1.Button9Click(Sender: TObject);
var i,j:integer;
    addition,sum:double;
begin
  n:=strtoint(edit1.Text) ;
  sum:=0;

  //方法1 迴圈
  for i:=1 to n do
  begin
    addition := 0;
    for j:=1 to i do
      addition := addition + 1/j;
    sum := sum + addition;
  end;
  Math09.Caption := format('%.7f',[sum]);

  //方法2 遞迴
  sum := recurs9(n);
  showmessage(format('%.7f',[sum]));
end;

//----------------------------------------------------------
//(10) 1+1/2+2/3+3/4+4/5...+(n-1)/n, 當項數(n) = 20 值為多少?

function recurs10(n:integer):double;
var ans : double;
begin
  if n > 1 then
    ans := (n-1)/n + recurs10(n-1)
  else
    ans := 1;
  result:=ans;
end;

procedure TForm1.Button10Click(Sender: TObject);
var i,j : integer;
    addition,sum : double;
begin
  n:=strtoint(edit1.Text) ;
  sum:=0;

  //方法1 迴圈
  for i := 1 to n do
  begin
    addition := 0;
    for j:=1 to i do
    begin
      if j > 1 then
        addition := (j-1)/j
      else
        addition := 1;
    end;
    sum := sum + addition;
  end;
  Math10.Caption := format('%.7f',[sum]);

  //方法2 遞迴
  sum := recurs10(n);
  showmessage(format('%.7f',[sum]));
end;


沒有留言:

張貼留言