Harmonic series: Difference between revisions

no edit summary
No edit summary
Line 529:
4550 9.000208060802
12367 10.000043002313</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function HarmonicNumber(N: integer): double;
{Calculate sum of }
var I: integer;
begin
Result:=0;
for I:=1 to N do Result:=Result+1/I;
end;
 
 
 
function FirstHarmonicOver(Limit: integer): integer;
{Find first harmonic number over limit}
var HN: double;
begin
for Result:=1 to high(Integer) do
begin
HN:=HarmonicNumber(Result);
if HN>Limit then exit;
end
end;
 
 
procedure ShowHarmonicNumbers(Memo: TMemo);
var I,Inx: integer;
var HN: double;
begin
{Show first 20 harmonic numbers}
for I:=1 to 20 do
begin
HN:=HarmonicNumber(I);
Memo.Lines.Add(Format('%2D: %8.8f',[I,HN]));
end;
{Show the position of the number that exceeds 1..10 }
for I:=1 to 10 do
begin
Inx:=FirstHarmonicOver(I);
Memo.Lines.Add(Format('Position of the first harmonic number > %2D: %4D',[I,Inx]))
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
1: 1.00000000
2: 1.50000000
3: 1.83333333
4: 2.08333333
5: 2.28333333
6: 2.45000000
7: 2.59285714
8: 2.71785714
9: 2.82896825
10: 2.92896825
11: 3.01987734
12: 3.10321068
13: 3.18013376
14: 3.25156233
15: 3.31822899
16: 3.38072899
17: 3.43955252
18: 3.49510808
19: 3.54773966
20: 3.59773966
Position of the first harmonic number > 1: 2
Position of the first harmonic number > 2: 4
Position of the first harmonic number > 3: 11
Position of the first harmonic number > 4: 31
Position of the first harmonic number > 5: 83
Position of the first harmonic number > 6: 227
Position of the first harmonic number > 7: 616
Position of the first harmonic number > 8: 1674
Position of the first harmonic number > 9: 4550
Position of the first harmonic number > 10: 12367
</pre>
 
 
=={{header|Factor}}==
465

edits