Sylvester's sequence: Difference between revisions

Add C# implementation
(add RPL)
(Add C# implementation)
 
(6 intermediate revisions by 5 users not shown)
Line 261:
end
</syntaxhighlight>
 
=={{header|C#}}==
{{trans|Go}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Numerics;
 
public class SylvesterSequence
{
public static void Main(string[] args)
{
BigInteger one = BigInteger.One;
BigInteger two = new BigInteger(2);
List<BigInteger> sylvester = new List<BigInteger> { two };
BigInteger prod = two;
int count = 1;
 
while (count < 10)
{
BigInteger next = BigInteger.Add(prod, one);
sylvester.Add(next);
count++;
prod *= next;
}
 
Console.WriteLine("The first 10 terms in the Sylvester sequence are:");
foreach (var term in sylvester)
{
Console.WriteLine(term);
}
 
// Assuming a BigRational implementation or a workaround for the sum of reciprocals
BigInteger denominator = BigInteger.One;
foreach (var term in sylvester)
{
denominator = BigInteger.Multiply(denominator, term);
}
 
BigInteger numerator = BigInteger.Zero;
foreach (var term in sylvester)
{
numerator += denominator / term;
}
 
// Assuming you have a way to convert this to a decimal representation
Console.WriteLine("\nThe sum of their reciprocals as a rational number is:");
Console.WriteLine($"{numerator}/{denominator}");
 
// For the decimal representation, you might need to perform the division to a fixed number of decimal places
// This is a simplified approach and may not directly achieve 211 decimal places accurately
Console.WriteLine("\nThe sum of their reciprocals as a decimal number (to 211 places) is:");
Console.WriteLine(DecimalDivide(numerator, denominator, 210));
}
 
private static string DecimalDivide(BigInteger numerator, BigInteger denominator, int decimalPlaces)
{
// This is a basic implementation and might not be accurate for very large numbers or very high precision requirements
BigInteger quotient = BigInteger.Divide(numerator * BigInteger.Pow(10, decimalPlaces + 1), denominator);
string quotientStr = quotient.ToString();
string result = quotientStr.Substring(0, quotientStr.Length - decimalPlaces) + "." + quotientStr.Substring(quotientStr.Length - decimalPlaces);
return result;
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 10 terms in the Sylvester sequence are:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
 
The sum of their reciprocals as a rational number is:
27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805/27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806
 
The sum of their reciprocals as a decimal number (to 211 places) is:
9.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999634
 
</pre>
 
 
Line 304 ⟶ 390:
Sum of reciprocals: 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805/27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
numfmt 8 0
for i = 1 to 10
if i = 1
sylv = 2
else
sylv = sylv * sylv - sylv + 1
.
print sylv
sum += 1 / sylv
.
print ""
print sum
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 488 ⟶ 590:
<syntaxhighlight lang="haskell">sylvester :: [Integer]
sylvester = iterate (succ . ((*) <*> pred)) 2</syntaxhighlight>
 
=={{header|J}}==
J uses r instead of / to display rationals
 
<syntaxhighlight lang="j"> 2 ('Sum of reciprocals: ' , ":@:(+/))@:%@:([ echo&>)@:((, 1x + */)@[&_~) 9
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of reciprocals: 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805r27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806</syntaxhighlight>
 
=={{header|jq}}==
Line 552 ⟶ 670:
<pre>{2,3,7,43,1807,3263443,10650056950807,113423713055421844361000443,12864938683278671740537145998360961546653259485195807,165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443}
0.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999996349359079841301329356159748234615361531272</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
sylvester[n]:= if n=0 then sylvester[n]:2 else sylvester[n-1]^2-sylvester[n-1]+1$
 
/* Test cases */
makelist(sylvester[i],i,0,9);
 
apply("+",1/%);
</syntaxhighlight>
{{out}}
<pre>
[2,3,7,43,1807,3263443,10650056950807,113423713055421844361000443,12864938683278671740537145998360961546653259485195807,165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443]
 
27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805/27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806
</pre>
 
=={{header|Nim}}==
Line 597 ⟶ 731:
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>
 
Line 1,267 ⟶ 1,456:
=={{header|Wren}}==
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt, BigRat
 
var sylvester = [BigInt.two]
337

edits