全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2539
推到 Plurk!
推到 Facebook!

調和級數之和

 
christie
資深會員


發表:30
回覆:299
積分:475
註冊:2005-03-25

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-10-14 17:42:32 IP:218.171.xxx.xxx 訂閱
求調和級數(harmonics)之和
\begin{displaymath}
1 \frac{1}{2} \frac{1}{3} \frac{1}{4} \cdots
\end{displaymath}

[code delphi]
{ 調和級數之和, 以最簡分數表示
1 1 1
H(n) = 1 - - ... -
2 3 n
}
program sumHarmonic;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
firstterm = 2;
var
numerator, denominator,
lastterm, termcount : integer;
{ Divide num and den by their GCD. }
procedure lowterm(var num, den :integer);
var
numcopy, dencopy, remainder : integer;
begin
numcopy:=num;
dencopy:=den;
while dencopy<>0 do
begin
remainder := numcopy mod dencopy;
numcopy := dencopy;
dencopy := remainder;
end;
if numcopy > 1 then
begin
num := num div numcopy;
den := den div numcopy;
end;
end; { lowterm }
{ Calculate the exact sum of two rational numbers }
procedure addrationals(var num1, den1:integer;
num2, den2 : integer);
begin
num1:=num1 * den2 num2 * den1;
den1:=den1 * den2;
end;
begin { sumHarmonics }

numerator :=1;
denominator:=1;
read(lastterm);
for termcount:=firstterm to lastterm do
begin
addrationals(numerator, denominator, 1, termcount);
lowterm(numerator, denominator);
writeln(numerator:4 , ' / ', denominator:4)
end;
end. { sumHarmonics }

[/code]
------
What do we live for if not to make life less difficult for each other?
系統時間:2024-12-04 1:27:46
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!