Sylvester's sequence: Difference between revisions

Added solution for Pascal
(add RPL)
(Added solution for Pascal)
Line 597:
27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805/27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806
 
</pre>
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
Free Pascal console program, using the library IntXLib4Pascal for arbitrarily large integers. I couldn't get the library to work with Delphi 7; it's said to work with Delphi 2010 and above.
<syntaxhighlight lang="pascal">
program SylvesterSeq;
 
{$mode objfpc}{$H+}
 
uses SysUtils,
UIntX; // in the library IntX4Pascal
(*
As noted in the Wikipedia article "Sylvester's sequence", we have
1/2 + 1/3 + ... + 1/s[j-1] = (s[j] - 2)/(s[j] - 1),
so that instead of summing the reciprocals explicitly we can just
calculate an extra term.
*)
var
s : UIntX.TIntX; // arbitrarily large integer
i : integer;
begin
s := 1;
for i := 0 to 9 do begin
inc(s);
WriteLn( SysUtils.Format( 's[%d] = %s', [i, s.ToString]));
s := s*(s - 1);
end;
WriteLn( 'Sum of reciprocals =');
WriteLn( (s - 1).ToString);
WriteLn( '/'); // on a separate line for clarity
WriteLn( s.ToString);
end.
</syntaxhighlight>
{{out}}
<pre>
s[0] = 2
s[1] = 3
s[2] = 7
s[3] = 43
s[4] = 1807
s[5] = 3263443
s[6] = 10650056950807
s[7] = 113423713055421844361000443
s[8] = 12864938683278671740537145998360961546653259485195807
s[9] = 1655066473245199641984681954444391800175131527063774978418513887665358686
39572406808911988131737645185443
Sum of reciprocals =
27392450308603031423410234291674686281194364367580914627947367941608692026226993
63433211840458243863492954873728399236975848797430631773058075388342946034495641
0077034761330476016739454649828385541500213920805
/
27392450308603031423410234291674686281194364367580914627947367941608692026226993
63433211840458243863492954873728399236975848797430631773058075388342946034495641
0077034761330476016739454649828385541500213920806
</pre>
 
113

edits