Sylvester's sequence: Difference between revisions
Content deleted Content added
→{{header|Python}}: Added a functionally composed solution in Python. |
Add C# implementation |
||
(46 intermediate revisions by 28 users not shown) | |||
Line 1:
{{
{{Wikipedia|Sylvester's sequence}}
In number theory, '''Sylvester's sequence''' is an integer sequence in which each term of the sequence is the product of the previous terms, plus one.
Its values grow doubly exponentially, and the sum of its reciprocals forms a series of unit fractions that converges to '''1''' more rapidly than any other series of unit fractions with the same number of terms.
Further, the sum of the first '''k''' terms of the infinite series of reciprocals provides the closest possible underestimate of '''1''' by any k-term Egyptian fraction.
;Task:
* Write a routine (function, procedure, generator, whatever) to calculate '''Sylvester's sequence'''.
* Use that routine to show the values of the first '''10''' elements in the sequence.
* Show the sum of the reciprocals of the first '''10''' elements on the sequence, ideally as an exact fraction.
;Related tasks:
* [[Egyptian fractions]]
* [[Harmonic series]]
;See also:
* [[oeis:A000058|OEIS A000058 - Sylvester's sequence]]
<br>
=={{header|11l}}==
{{trans|Nim}}
<syntaxhighlight lang="11l">F sylverster(lim)
V result = [BigInt(2)]
L 2..lim
result.append(product(result) + 1)
R result
V l = sylverster(10)
print(‘First 10 terms of the Sylvester sequence:’)
L(item) l
print(item)
V s = 0.0
L(item) l
s += 1 / Float(item)
print("\nSum of the reciprocals of the first 10 terms: #.17".format(s))</syntaxhighlight>
{{out}}
<pre>
First 10 terms of the Sylvester sequence:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of the reciprocals of the first 10 terms: 0.99999999999999982
</pre>
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses Algol 68G's LONG LONG INT and LONG LONG REAL which have specifiable precision. The sum of the
<
PR precision 200 PR # set the number of digits for LONG LONG modes #
# returns an array set to the forst n elements of Sylvestor's Sequence #
Line 44 ⟶ 82:
# find the first 10 elements of Sylvestor's Seuence #
[]LONG LONG INT seq = SYLVESTOR 10;
# show the sequence and sum the
LONG LONG REAL
FOR i FROM LWB seq TO UPB seq DO
print( ( whole( seq[ i ], 0 ), newline ) );
OD;
print( ( "Sum of
END
</syntaxhighlight>
{{out}}
<pre>
Line 65 ⟶ 103:
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">sylvester: function [lim][
result: new [2]
loop 2..lim 'x [
'result ++ inc fold result .seed:1 [a b][a * b]
]
return result
]
lst: sylvester 10
print "First 10 terms of the Sylvester sequence:"
print lst
print ""
sumRep: round sum map lst => [1 // &]
print "Sum of the reciprocals of the first 10 items:"
print sumRep</syntaxhighlight>
{{out}}
<pre>First 10 terms of the Sylvester sequence:
2 3 7 43 1807 3263443 10650056950807 113423713055421844361000443 12864938683278671740537145998360961546653259485195807 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of the reciprocals of the first 10 items:
1.0</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK --bignum -f SYLVESTERS_SEQUENCE.AWK
BEGIN {
start = 1
stop = 10
for (i=start; i<=stop; i++) {
sylvester = (i == 1) ? 2 : sylvester*sylvester-sylvester+1
printf("%2d: %d\n",i,sylvester)
sum += 1 / sylvester
}
printf("\nSylvester sequence %d-%d: sum of reciprocals %30.28f\n",start,stop,sum)
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
1: 2
2: 3
3: 7
4: 43
5: 1807
6: 3263443
7: 10650056950807
8: 113423713055421844361000443
9: 12864938683278671740537145998360961546653259485195807
10: 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sylvester sequence 1-10: sum of reciprocals 0.9999999999999998889776975375
</pre>
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{works with|True BASIC}}
{{works with|Chipmunk Basic}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">
PRINT "10 primeros términos de la sucesión de sylvester:"
PRINT
LET suma = 0
FOR i = 1 to 10
IF i = 1 then
LET sylvester = 2
ELSE
LET sylvester = sylvester*sylvester-sylvester+1
END IF
PRINT i; ": "; sylvester
LET suma = suma + 1 / sylvester
NEXT i
PRINT
PRINT "suma de sus recíprocos: "; suma
END
</syntaxhighlight>
==={{header|FreeBASIC}}===
'''precisión estándar'''
<syntaxhighlight lang="freebasic">
Dim As Double sylvester, suma = 0
Print "10 primeros t‚rminos de la sucesi¢n de Sylvester:"
For i As Byte = 1 To 10
sylvester = Iif(i=1, 2, sylvester*sylvester-sylvester+1)
Print Using "##: &"; i; sylvester
suma += 1 / sylvester
Next i
Print !"\nSuma de sus rec¡procos:"; suma
Sleep
</syntaxhighlight>
{{out}}
<pre>
10 primeros términos de la sucesión de Sylvester:
1: 2
2: 3
3: 7
4: 43
5: 1807
6: 3263443
7: 10650056950807
8: 1.134237130554218E+26
9: 1.286493868327867E+52
10: 1.6550664732452E+104
Suma de sus recíprocos: 0.9999999999999999
</pre>
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">
OpenConsole()
PrintN("10 primeros términos de la sucesión de Sylvester:")
suma.d = 0
For i.i = 1 To 10
If i = 1
sylvester.d = 2
Else
sylvester.d = sylvester*sylvester-sylvester+1
EndIf
PrintN(Str(i) + ": " + StrD(sylvester))
suma = suma + 1 / sylvester
Next i
Print(#CRLF$ + "Suma de sus recíprocos: " + StrD(suma))
Input()
CloseConsole()
End
</syntaxhighlight>
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">
print "10 primeros términos de la sucesión de Sylvester:"
suma = 0
for i = 1 to 10
if i=1 then sylvester = 2 else sylvester = sylvester*sylvester-sylvester+1 : fi
print i using("##"), ": ", sylvester
suma = suma + 1 / sylvester
next i
print "\nSuma de sus rec¡procos: ", suma
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>
=={{header|C++}}==
{{libheader|Boost}}
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using integer = boost::multiprecision::cpp_int;
using rational = boost::rational<integer>;
integer sylvester_next(const integer& n) {
return n * n - n + 1;
}
int main() {
std::cout << "First 10 elements in Sylvester's sequence:\n";
integer term = 2;
rational sum = 0;
for (int i = 1; i <= 10; ++i) {
std::cout << std::setw(2) << i << ": " << term << '\n';
sum += rational(1, term);
term = sylvester_next(term);
}
std::cout << "Sum of reciprocals: " << sum << '\n';
}</syntaxhighlight>
{{out}}
<pre>
First 10 elements in Sylvester's sequence:
1: 2
2: 3
3: 7
4: 43
5: 1807
6: 3263443
7: 10650056950807
8: 113423713055421844361000443
9: 12864938683278671740537145998360961546653259485195807
10: 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
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#}}==
<syntaxhighlight lang="fsharp">
// Sylvester's sequence: Nigel Galloway. June 7th., 2021
let S10=Seq.unfold(fun(n,g)->printfn "*%A %A" n g; Some(n,(n*g+1I,n*g) ) )(2I,1I)|>Seq.take 10|>List.ofSeq
S10|>List.iteri(fun n g->printfn "%2d -> %A" (n+1) g)
let n,g=S10|>List.fold(fun(n,g) i->(n*i+g,g*i))(0I,1I) in printfn "\nThe sum of the reciprocals of S10 is \n%A/\n%A" n g
</syntaxhighlight>
{{out}}
<pre>
1 -> 2
2 -> 3
3 -> 7
4 -> 43
5 -> 1807
6 -> 3263443
7 -> 10650056950807
8 -> 113423713055421844361000443
9 -> 12864938683278671740537145998360961546653259485195807
10 -> 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
The sum of the reciprocals of S10 is
27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805/
27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806
</pre>
Line 72 ⟶ 436:
{{works with|Factor|0.99 2021-02-05}}
<
: lsylvester ( -- list ) 2 [ dup sq swap - 1 + ] lfrom-by ;
Line 80 ⟶ 444:
"Sum of the reciprocals of first 10 elements:" print
0 [ recip + ] foldl .</
{{out}}
<pre>
Line 100 ⟶ 464:
Or, in other words, the sum is <code>2739245…392080'''5/'''2739245…392080'''6'''</code>.
=={{header|Fermat}}==
<syntaxhighlight lang="fermat">Array syl[10];
syl[1]:=2;
for i=2 to 10 do syl[i]:=1+Prod<n=1,i-1>[syl[n]] od;
!![syl];
srec:=Sigma<i=1,10>[1/syl[i]];
!!srec;</syntaxhighlight>
{{out}}<pre>
syl[1] := 2
syl[2] := 3
syl[3] := 7
syl[4] := 43
syl[5] := 1807
syl[6] := 3263443
syl[7] := 10650056950807
syl[8] := 113423713055421844361000443
syl[9] := 12864938683278671740537145998360961546653259485195807
syl[10] := 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
2739245030860303142341023429167468628119436436758091462794736794160869202622699363433211840458243863492954873728399236975 `
8487974306317730580753883429460344956410077034761330476016739454649828385541500213920805 / 273924503086030314234102342916746 `
862811943643675809146279473679416086920262269936343321184045824386349295487372839923697584879743063177305807538834294603 `
44956410077034761330476016739454649828385541500213920806
</pre>
=={{header|Go}}==
{{trans|Wren}}
<syntaxhighlight lang="go">package main
import (
"fmt"
"math/big"
)
func main() {
one := big.NewInt(1)
two := big.NewInt(2)
next := new(big.Int)
sylvester := []*big.Int{two}
prod := new(big.Int).Set(two)
count := 1
for count < 10 {
next.Add(prod, one)
sylvester = append(sylvester, new(big.Int).Set(next))
count++
prod.Mul(prod, next)
}
fmt.Println("The first 10 terms in the Sylvester sequence are:")
for i := 0; i < 10; i++ {
fmt.Println(sylvester[i])
}
sumRecip := new(big.Rat)
for _, s := range sylvester {
sumRecip.Add(sumRecip, new(big.Rat).SetFrac(one, s))
}
fmt.Println("\nThe sum of their reciprocals as a rational number is:")
fmt.Println(sumRecip)
fmt.Println("\nThe sum of their reciprocals as a decimal number (to 211 places) is:")
fmt.Println(sumRecip.FloatString(211))
}</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:
0.9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999635
</pre>
=={{header|Haskell}}==
<
sylvester = map s [0 ..]
where
Line 117 ⟶ 565:
putStr "Sum of reciprocals by fold: "
print $ foldr ((+) . (1 /) . fromInteger) 0 $ take 10 sylvester</
{{out}}
<pre>First 10 elements of Sylvester's sequence:
Line 134 ⟶ 582:
Sum of reciprocals by fold: 1.0</pre>
Simpler way of generating sequence:
<syntaxhighlight lang="haskell">sylvester :: [Integer]
sylvester = iterate (\x -> x * (x-1) + 1) 2</syntaxhighlight>
or applicatively:
<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}}==
<syntaxhighlight lang="jq"># Generate the sylvester integers:
def sylvester:
foreach range(0; infinite) as $i ({prev: 1, product: 1};
.product *= .prev
| .prev = .product + 1;
.prev);</syntaxhighlight>
Left padding:
<syntaxhighlight lang="jq">
def lpad($len; $fill): tostring | ($len - length) as $l | ($fill * $l)[:$l] + .;
def lpad($len): lpad($len; " ");
def lpad: lpad(4);</syntaxhighlight>
The task:
<syntaxhighlight lang="jq">[limit(10; sylvester)]
| "First 10 Sylvester numbers:",
(range(0;10) as $i | "\($i+1|lpad) => \(.[$i])"),
"",
"Sum of reciprocals of first 10 is approximately: \(map( 1/ .) | add)"
</syntaxhighlight>
{{out}}
For integer precision, we will use `gojq`, the "go" implementation of jq.
<pre>First 10 Sylvester numbers:
1 => 2
2 => 3
3 => 7
4 => 43
5 => 1807
6 => 3263443
7 => 10650056950807
8 => 113423713055421844361000443
9 => 12864938683278671740537145998360961546653259485195807
10 => 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of reciprocals of first 10 is approximately: 0.9999999999999999</pre>
=={{header|Julia}}==
<syntaxhighlight lang="julia">sylvester(n) = (n == 1) ? big"2" : prod(sylvester, 1:n-1) + big"1"
foreach(n -> println(rpad(n, 3), " => ", sylvester(n)), 1:10)
println("Sum of reciprocals of first 10: ", sum(big"1.0" / sylvester(n) for n in 1:10))
</
<pre>
1 => 2
Line 154 ⟶ 662:
Sum of reciprocals of first 10: 0.9999999999999999999999999999999999999999999999999999999999999999999999999999914
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Rest[Nest[Append[#, (Times @@ #) + 1] &, {1}, 10]]
N[Total[1/%], 250]</syntaxhighlight>
{{out}}
<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}}==
{{libheader|bignum}}
<syntaxhighlight lang="nim">import sequtils
import bignum
proc sylverster(lim: Positive): seq[Int] =
result.add(newInt(2))
for _ in 2..lim:
result.add result.foldl(a * b) + 1
let list = sylverster(10)
echo "First 10 terms of the Sylvester sequence:"
for item in list: echo item
var sum = newRat()
for item in list: sum += newRat(1, item)
echo "\nSum of the reciprocals of the first 10 terms: ", sum.toFloat</syntaxhighlight>
{{out}}
<pre>First 10 terms of the Sylvester sequence:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of the reciprocals of the first 10 terms: 0.9999999999999999</pre>
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">
S=vector(10)
S[1]=2
for(i=2, 10, S[i]=prod(n=1,i-1,S[n])+1)
print(S)
print(sum(i=1,10,1/S[i]))</syntaxhighlight>
{{out}}<pre>[2, 3, 7, 43, 1807, 3263443, 10650056950807, 113423713055421844361000443, 12864938683278671740537145998360961546653259485195807, 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443]
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>
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 170 ⟶ 802:
say "First 10 elements of Sylvester's sequence: @S";
say "\nSum of the reciprocals of first 10 elements: " . float $sum;</
{{out}}
<pre>First 10 elements of Sylvester's sequence: 2 3 7 43 1807 3263443 10650056950807 113423713055421844361000443 12864938683278671740537145998360961546653259485195807 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Line 178 ⟶ 810:
=={{header|Phix}}==
=== standard precision ===
<!--<
<span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?</span><span style="color: #000000;">53</span><span style="color: #0000FF;">:</span><span style="color: #000000;">64</span><span style="color: #0000FF;">))</span>
Line 187 ⟶ 819:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sum of reciprocals: %g\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">rn</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 204 ⟶ 836:
=== mpfr version ===
Note the (minimal) precision settings of 698 and 211 were found by trial and error (ie larger values gain nothing but smaller ones lose accuracy).
<!--<
<span style="color: #008080;">
<span style="color: #7060A8;">
-- (and mpfr_sprintf() replaced with mpfr_get_fixed())</span>
<span style="color: #
<span style="color: #004080;">mpz</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">nm1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(</span><span style="color: #000000;">720</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">rn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
Line 216 ⟶ 851:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sum of reciprocals: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">
<!--</
{{out}}
<pre>
Line 235 ⟶ 870:
10: 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
sum of reciprocals: 0.999999999999999999...99999999999999999635 (213 digits)
</pre>
=={{header|PL/M}}==
As the original 8080 PL/M only has unsigned 8 and 16 bit items, this Uses code from the PL/M [[Long Multiplication]] sample routines.
<br>It doesn't calculate the reciprocal sum as 8080 PL/M has no floating point...
<br>This sample can be compiled with the original 8080 PL/M compiler and run under CP/M (or an emulator or clone).
<syntaxhighlight lang="plm">100H: /* CALCULATE ELEMENTS OF SYLVESTOR'S SEQUENCE */
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
DECLARE FN BYTE, ARG ADDRESS;
GOTO 5;
END BDOS;
PRINT$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PRINT$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
DECLARE PRINT$NL LITERALLY 'PRINT$STRING( .( 0DH, 0AH, ''$'' ) )';
DECLARE LONG$INTEGER LITERALLY '(201)BYTE';
DECLARE DIGIT$BASE LITERALLY '10';
/* PRINTS A LONG INTEGER */
PRINT$LONG$INTEGER: PROCEDURE( N$PTR );
DECLARE N$PTR ADDRESS;
DECLARE N BASED N$PTR LONG$INTEGER;
DECLARE ( D, F ) BYTE;
F = N( 0 );
DO D = 1 TO N( 0 );
CALL PRINT$CHAR( N( F ) + '0' );
F = F - 1;
END;
END PRINT$LONG$INTEGER;
/* IMPLEMENTS LONG MULTIPLICATION, C IS SET TO A * B */
/* C CAN BE THE SAME LONG$INTEGER AS A OR B */
LONG$MULTIPLY: PROCEDURE( A$PTR, B$PTR, C$PTR );
DECLARE ( A$PTR, B$PTR, C$PTR ) ADDRESS;
DECLARE ( A BASED A$PTR, B BASED B$PTR, C BASED C$PTR ) LONG$INTEGER;
DECLARE MRESULT LONG$INTEGER;
DECLARE RPOS BYTE;
/* MULTIPLIES THE LONG INTEGER IN B BY THE INTEGER A, THE RESULT */
/* IS ADDED TO C, STARTING FROM DIGIT START */
/* OVERFLOW IS IGNORED */
MULTIPLY$ELEMENT: PROCEDURE( A, B$PTR, C$PTR, START );
DECLARE ( B$PTR, C$PTR ) ADDRESS;
DECLARE ( A, START ) BYTE;
DECLARE ( B BASED B$PTR, C BASED C$PTR ) LONG$INTEGER;
DECLARE ( CDIGIT, D$CARRY, BPOS, CPOS ) BYTE;
D$CARRY = 0;
CPOS = START;
DO BPOS = 1 TO B( 0 );
CDIGIT = C( CPOS ) + ( A * B( BPOS ) ) + D$CARRY;
IF CDIGIT < DIGIT$BASE THEN D$CARRY = 0;
ELSE DO;
/* HAVE DIGITS TO CARRY */
D$CARRY = CDIGIT / DIGIT$BASE;
CDIGIT = CDIGIT MOD DIGIT$BASE;
END;
C( CPOS ) = CDIGIT;
CPOS = CPOS + 1;
END;
C( CPOS ) = D$CARRY;
/* REMOVE LEADING ZEROS BUT IF THE NUMBER IS 0, KEEP THE FINAL 0 */
DO WHILE( CPOS > 1 AND C( CPOS ) = 0 );
CPOS = CPOS - 1;
END;
C( 0 ) = CPOS;
END MULTIPLY$ELEMENT ;
/* THE RESULT WILL BE COMPUTED IN MRESULT, ALLOWING A OR B TO BE C */
DO RPOS = 1 TO LAST( MRESULT ); MRESULT( RPOS ) = 0; END;
/* MULTIPLY BY EACH DIGIT AND ADD TO THE RESULT */
DO RPOS = 1 TO A( 0 );
IF A( RPOS ) <> 0 THEN DO;
CALL MULTIPLY$ELEMENT( A( RPOS ), B$PTR, .MRESULT, RPOS );
END;
END;
/* RETURN THE RESULT IN C */
DO RPOS = 0 TO MRESULT( 0 ); C( RPOS ) = MRESULT( RPOS ); END;
END;
/* ADDS THE INTEGER A TO THE LONG$INTEGER N */
ADD$BYTE$TO$LONG$INTEGER: PROCEDURE( A, N$PTR );
DECLARE A BYTE, N$PTR ADDRESS;
DECLARE N BASED N$PTR LONG$INTEGER;
DECLARE ( D, D$CARRY, DIGIT ) BYTE;
D = 1;
D$CARRY = A;
DO WHILE( D$CARRY > 0 );
DIGIT = N( D ) + D$CARRY;
IF DIGIT < DIGIT$BASE THEN DO;
N( D ) = DIGIT;
D$CARRY = 0;
END;
ELSE DO;
D$CARRY = DIGIT / DIGIT$BASE;
N( D ) = DIGIT MOD DIGIT$BASE;
D = D + 1;
IF D > N( 0 ) THEN DO;
/* THE NUMBER NOW HAS AN EXTRA DIGIT */
N( 0 ) = D;
N( D ) = D$CARRY;
D$CARRY = 0;
END;
END;
END;
END ADD$BYTE$TO$LONG$INTEGER;
/* FIND THE FIRST 10 ELEMENTS OF SYLVESTOR'S SEQUENCE */
DECLARE ( SEQ$ELEMENT, PRODUCT ) LONG$INTEGER;
DECLARE ( I, D ) BYTE;
DO D = 2 TO LAST( PRODUCT ); PRODUCT( D ) = 0; END;
DO D = 2 TO LAST( SEQ$ELEMENT ); SEQ$ELEMENT( D ) = 0; END;
SEQ$ELEMENT( 0 ) = 1; /* THE FIRST SEQUENCE ELEMENT HAS 1 DIGIT... */
SEQ$ELEMENT( 1 ) = 2; /* WHICH IS 2 */
PRODUCT( 0 ) = 1;
PRODUCT( 1 ) = 2;
CALL PRINT$LONG$INTEGER( .SEQ$ELEMENT ); /* SHOW ELEMENT 1 */
CALL PRINT$NL;
DO I = 2 TO 9;
DO D = 0 TO PRODUCT( 0 ); SEQ$ELEMENT( D ) = PRODUCT( D ); END;
CALL ADD$BYTE$TO$LONG$INTEGER( 1, .SEQ$ELEMENT );
CALL PRINT$LONG$INTEGER( .SEQ$ELEMENT );
CALL LONG$MULTIPLY( .SEQ$ELEMENT, .PRODUCT, .PRODUCT );
CALL PRINT$NL;
END;
/* THE FINAL ELEMENT IS THE PRODUCT PLUS 1 */
CALL ADD$BYTE$TO$LONG$INTEGER( 1, .PRODUCT );
CALL PRINT$LONG$INTEGER( .PRODUCT );
CALL PRINT$NL;
EOF</syntaxhighlight>
{{out}}
<pre>
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
</pre>
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">sylvesters_sequence(N, S, R):-
sylvesters_sequence(N, S, 2, R, 0).
sylvesters_sequence(0, [X], X, R, S):-
!,
R is S + 1 rdiv X.
sylvesters_sequence(N, [X|Xs], X, R, S):-
Y is X * X - X + 1,
M is N - 1,
T is S + 1 rdiv X,
sylvesters_sequence(M, Xs, Y, R, T).
main:-
sylvesters_sequence(9, Sequence, Sum),
writeln('First 10 elements in Sylvester\'s sequence:'),
forall(member(S, Sequence), writef('%t\n', [S])),
N is numerator(Sum),
D is denominator(Sum),
writef('\nSum of reciprocals: %t / %t\n', [N, D]).</syntaxhighlight>
{{out}}
<pre>
First 10 elements in Sylvester's sequence:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of reciprocals: 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805 / 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806
</pre>
=={{header|Python}}==
<
from functools import reduce
Line 280 ⟶ 1,093:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>First 10 terms of OEIS A000058:
Line 296 ⟶ 1,109:
Sum of the reciprocals of the first 10 terms:
0.9999999999999999</pre>
Or as an iteration:
<syntaxhighlight lang="python">'''Sylvester's sequence'''
from functools import reduce
from itertools import islice
# sylvester :: [Int]
def sylvester():
'''A non finite sequence of the terms of OEIS A000058
'''
return iterate(
lambda x: x * (x - 1) + 1
)(2)
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First terms, and sum of reciprocals.'''
print("First 10 terms of OEIS A000058:")
xs = list(islice(sylvester(), 10))
print('\n'.join([
str(x) for x in xs
]))
print("\nSum of the reciprocals of the first 10 terms:")
print(
reduce(lambda a, x: a + 1 / x, xs, 0)
)
# ----------------------- GENERIC ------------------------
# iterate :: (a -> a) -> a -> Gen [a]
def iterate(f):
'''An infinite list of repeated
applications of f to x.
'''
def go(x):
v = x
while True:
yield v
v = f(v)
return go
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre>First 10 terms of OEIS A000058:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of the reciprocals of the first 10 terms:
0.9999999999999999</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="quackery">[ $ "bigrat.qky" loadfile ] now!
' [ 2 ] 9 times [ dup -1 peek dup 2 ** swap - 1+ join ]
dup witheach [ echo cr ] cr
0 n->v rot witheach [ n->v 1/v v+ ] 222 point$ echo$</syntaxhighlight>
{{out}}
The first 222 digits after the decimal point are shown for the sum of reciprocals.
<pre>2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
0.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999963493590798413
</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku"
put 'First 10 elements of Sylvester\'s sequence: ', @S[^10];
say "\nSum of the reciprocals of first 10 elements: ", sum @S[^10].map: { FatRat.new: 1, $_ };</
{{out}}
<pre>First 10 elements of Sylvester's sequence: 2 3 7 43 1807 3263443 10650056950807 113423713055421844361000443 12864938683278671740537145998360961546653259485195807 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of the reciprocals of first 10 elements: 0.9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999635</pre>
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX pgm finds N terms of the Sylvester's sequence & the sum of the their reciprocals.*/
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 10 /*Not specified? Then use the default.*/
numeric digits max(9, 2**(n-7) * 13 + 1) /*calculate how many dec. digs we need.*/
@.0= 2 /*the value of the 1st Sylvester number*/
$= 0
do j=0 for n; jm= j - 1 /*calculate the Sylvester sequence. */
if j>0 then @.j= @.jm**2 - @.jm + 1 /*calculate a Sylvester sequence num.*/
say 'Sylvester('j") ──► " @.j /*display the Sylvester index & number.*/
$= $ + 1 / @.j /*add its reciprocal to the recip. sum.*/
end /*j*/
say /*stick a fork in it, we're all done. */
numeric digits digits() - 1
say 'sum of the first ' n " reciprocals using" digits() 'decimal digits: ' $ / 1</syntaxhighlight>
{{out|output|text= when using the default inputs:}}
<pre>
Sylvester(0) ──► 2
Sylvester(1) ──► 3
Sylvester(2) ──► 7
Sylvester(3) ──► 43
Sylvester(4) ──► 1807
Sylvester(5) ──► 3263443
Sylvester(6) ──► 10650056950807
Sylvester(7) ──► 113423713055421844361000443
Sylvester(8) ──► 12864938683278671740537145998360961546653259485195807
Sylvester(9) ──► 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
sum of the first 10 reciprocals using 104 decimal digits: 1
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
≪ { 2 3 }
'''DO'''
DUP ΠLIST 1 + +
'''UNTIL''' DUP2 SIZE == '''END'''
NIP
≫ '<span style="color:blue">SYLV</span>' STO
10 <span style="color:blue">SYLV</span>
DUP INV ∑LIST EXPAND
{{out}}
<pre>
2: { 2 3 7 43 1807 3263443 10650056950807 113423713055421844361000443 12864938683278671740537145998360961546653259485195807 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443 }
1: 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805 /
27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def sylvester(n) = (1..n).reduce(2){|a| a*a - a + 1 }
(0..9).each {|n| puts "#{n}: #{sylvester n}" }
puts "
Sum of reciprocals of first 10 terms:
#{(0..9).sum{|n| 1.0r / sylvester(n)}.to_f }"
</syntaxhighlight>
{{out}}
<pre>0: 2
1: 3
2: 7
3: 43
4: 1807
5: 3263443
6: 10650056950807
7: 113423713055421844361000443
8: 12864938683278671740537145998360961546653259485195807
9: 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of reciprocals of first 10 terms:
1.0
</pre>
=={{header|Scheme}}==
<syntaxhighlight lang="scheme">(define sylvester
(lambda (x)
(if (= x 1)
2
(let ((n (sylvester (- x 1)))) (- (* n n) n -1)))))
(define list (map sylvester '(1 2 3 4 5 6 7 8 9 10)))
(print list)
(newline)
(print (apply + (map / list)))</syntaxhighlight>
{{out}}
<pre>
(2 3 7 43 1807 3263443 10650056950807 113423713055421844361000443 12864938683278671740537145998360961546653259485195807 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443)
27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805/27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806
</pre>
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigint.s7i";
include "bigrat.s7i";
const func bigInteger: nextSylvester (in bigInteger: prev) is
return prev * prev - prev + 1_;
const proc: main is func
local
var bigInteger: number is 2_;
var bigRational: reciprocalSum is 0_ / 1_;
var integer: n is 0;
begin
writeln("First 10 elements of Sylvester's sequence:");
for n range 1 to 10 do
writeln(number);
reciprocalSum +:= 1_ / number;
number := nextSylvester(number);
end for;
writeln("\nSum of the reciprocals of the first 10 elements:");
writeln(reciprocalSum digits 210);
end func;</syntaxhighlight>
{{out}}
<pre>
First 10 elements of Sylvester's sequence:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of the reciprocals of the first 10 elements:
0.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999963
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func sylvester_sequence(n) {
1..n -> reduce({|a| a*(a-1) + 1 }, 2)
}
say "First 10 terms in Sylvester's sequence:"
10.of(sylvester_sequence).each_kv{|k,v| '%2s: %s' % (k,v) -> say }
say "\nSum of reciprocals of first 10 terms: "
say 10.of(sylvester_sequence).sum {|n| 1/n }.as_dec(230)</syntaxhighlight>
{{out}}
<pre>
First 10 terms in Sylvester's sequence:
0: 2
1: 3
2: 7
3: 43
4: 1807
5: 3263443
6: 10650056950807
7: 113423713055421844361000443
8: 12864938683278671740537145998360961546653259485195807
9: 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of reciprocals of first 10 terms:
0.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999996349359079841301329356
</pre>
=={{header|Swift}}==
Using mkrd's BigNumber library.
<syntaxhighlight lang="swift">import BigNumber
func sylvester(n: Int) -> BInt {
var a = BInt(2)
for _ in 0..<n {
a = a * a - a + 1
}
return a
}
var sum = BDouble(0)
for n in 0..<10 {
let syl = sylvester(n: n)
sum += BDouble(1) / BDouble(syl)
print(syl)
}
print("Sum of the reciprocals of first ten in sequence: \(sum)")</syntaxhighlight>
{{out}}
<pre>2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of the reciprocals of first ten in sequence: 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805/27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806</pre>
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">
module main;
integer i;
real suma, num;
initial begin
$display("10 primeros términos de la sucesión de sylvester:");
$display("");
suma = 0;
num = 0;
for(i=1; i<=10; i=i+1) begin
if (i==1) num = 2;
else num = num * num - num + 1;
$display(i, ": ", num);
suma = suma + 1 / num;
end
$display("");
$display("suma de sus recíprocos: ", suma);
$finish ;
end
endmodule
</syntaxhighlight>
{{out}}
<pre>10 primeros términos de la sucesión de sylvester:
1: 2.00000
2: 3.00000
3: 7.00000
4: 43.0000
5: 1807.00
6: 3.26344e+06
7: 1.06501e+13
8: 1.13424e+26
9: 1.28649e+52
10: 1.65507e+104
suma de sus recíprocos: 1.00000</pre>
=={{header|Wren}}==
{{libheader|Wren-big}}
<
var sylvester = [BigInt.two]
Line 329 ⟶ 1,475:
System.print (sumRecip)
System.print("\nThe sum of their reciprocals as a decimal number (to 211 places) is:")
System.print(sumRecip.toDecimal(211))</
{{out}}
|