Sylvester's sequence: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 222: Line 222:
Sum of reciprocals of first 10: 0.9999999999999999999999999999999999999999999999999999999999999999999999999999914
Sum of reciprocals of first 10: 0.9999999999999999999999999999999999999999999999999999999999999999999999999999914
</pre>
</pre>

=={{header|Nim}}==
{{libheader|bignum}}
<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</lang>

{{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|Perl}}==
=={{header|Perl}}==

Revision as of 17:31, 14 June 2021

Sylvester's sequence is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
This page uses content from Wikipedia. The original article was at Sylvester's sequence. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)


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;


See also


ALGOL 68

Works with: ALGOL 68G version 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 reciprocals in the output has been manually edited to replace a large number of nines with ... to reduce the width. <lang algol68>BEGIN # calculate elements of Sylvestor's Sequence #

   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 #
   #    starting from 2, the elements are the product of the previous     #
   #                     elements plus 1                                  #
   OP SYLVESTOR = ( INT n )[]LONG LONG INT:
      BEGIN
          [ 1 : n ]LONG LONG INT result;
          LONG LONG INT product := 2;
          result[ 1 ] := 2;
          FOR i FROM 2 TO n DO
              result[ i ] := product + 1;
              product *:= result[ i ]
          OD;
          result
      END;
   # find the first 10 elements of Sylvestor's Seuence #
   []LONG LONG INT seq = SYLVESTOR 10;
   # show the sequence and sum the reciprocals #
   LONG LONG REAL reciprocal sum := 0;
   FOR i FROM LWB seq TO UPB seq DO
       print( ( whole( seq[ i ], 0 ), newline ) );
       reciprocal sum +:= 1 / seq[ i ]
   OD;
   print( ( "Sum of reciprocals: ", reciprocal sum, newline ) )

END </lang>

Output:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of reciprocals: +9.99999999999999999999999999999999999999999...999999999999999999999999999964e  -1

C++

Library: Boost

<lang cpp>#include <iomanip>

  1. include <iostream>
  2. include <boost/rational.hpp>
  3. 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';

}</lang>

Output:
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

F#

<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 </lang>

Output:
 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

Factor

Note that if the previous element of the sequence is x, the next element is x2-x+1.

Works with: Factor version 0.99 2021-02-05

<lang factor>USING: io kernel lists lists.lazy math prettyprint ;

lsylvester ( -- list ) 2 [ dup sq swap - 1 + ] lfrom-by ;

"First 10 elements of Sylvester's sequence:" print 10 lsylvester ltake dup [ . ] leach nl

"Sum of the reciprocals of first 10 elements:" print 0 [ recip + ] foldl .</lang>

Output:
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:
27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805/27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806

Or, in other words, the sum is 2739245…3920805/2739245…3920806.

Haskell

<lang haskell>sylvester :: [Integer] sylvester = map s [0 ..]

 where
   s 0 = 2
   s n = succ $ foldr ((*) . s) 1 [0 .. pred n]

main :: IO () main = do

 putStrLn "First 10 elements of Sylvester's sequence:"
 putStr $ unlines $ map show $ take 10 sylvester
 putStr "\nSum of reciprocals by sum over map: "
 print $ sum $ map ((1 /) . fromInteger) $ take 10 sylvester
 putStr "Sum of reciprocals by fold: "
 print $ foldr ((+) . (1 /) . fromInteger) 0 $ take 10 sylvester</lang>
Output:
First 10 elements of Sylvester's sequence:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443

Sum of reciprocals by sum over map: 0.9999999999999999
Sum of reciprocals by fold: 1.0

Julia

<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))

</lang>

Output:
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: 0.9999999999999999999999999999999999999999999999999999999999999999999999999999914

Nim

Library: bignum

<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</lang>

Output:
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

Perl

<lang perl>use strict; use warnings; use feature 'say'; use List::Util 'reduce'; use Math::AnyNum ':overload'; local $Math::AnyNum::PREC = 845;

my(@S,$sum); push @S, 1 + reduce { $a * $b } @S for 0..10; shift @S; $sum += 1/$_ for @S;

say "First 10 elements of Sylvester's sequence: @S"; say "\nSum of the reciprocals of first 10 elements: " . float $sum;</lang>

Output:
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

Phix

standard precision

atom n, rn = 0,
     lim = power(2,iff(machine_bits()=32?53:64))
for i=1 to 10 do
    n = iff(i=1?2:n*n-n+1)
    printf(1,iff(n<=lim?"%d: %d\n":"%d: %g\n"),{i,n})
    rn += 1/n
end for
printf(1,"sum of reciprocals: %g\n",{rn})
Output:
1: 2
2: 3
3: 7
4: 43
5: 1807
6: 3263443
7: 10650056950807
8: 1.13424e+26
9: 1.2865e+52
10: 1.65507e+104
sum of reciprocals: 1

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).

include mpfr.e
mpz n = mpz_init(2), nm1 = mpz_init()
mpfr_set_default_prec(698)
mpfr {rn, tmp} = mpfr_inits(2)
for i=1 to 10 do
    if i>1 then
        mpz_sub_ui(nm1,n,1)
        mpz_mul(n,nm1,n)
        mpz_add_ui(n,n,1)
    end if
    printf(1,"%d: %s\n",{i,mpz_get_str(n)})
    mpfr_set_z(tmp,n)
    mpfr_si_div(tmp,1,tmp)
    mpfr_add(rn,rn,tmp)
end for
printf(1,"sum of reciprocals: %s\n",{shorten(mpfr_sprintf("%.211Rf",rn))})
Output:
1: 2
2: 3
3: 7
4: 43
5: 1807
6: 3263443
7: 10650056950807
8: 113423713055421844361000443
9: 12864938683278671740537145998360961546653259485195807
10: 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
sum of reciprocals: 0.999999999999999999...99999999999999999635 (213 digits)

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.
It doesn't calculate the reciprocal sum as 8080 PL/M has no floating point...
This sample can be compiled with the original 8080 PL/M compiler and run under CP/M (or an emulator or clone). <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</lang>

Output:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443

Prolog

Works with: SWI Prolog

<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]).</lang>
Output:
First 10 elements in Sylvester's sequence:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443

Sum of reciprocals: 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805 / 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806

Python

<lang python>Sylvester's sequence

from functools import reduce from itertools import count, islice


  1. sylvester :: [Int]

def sylvester():

   Non-finite stream of the terms
      of Sylvester's sequence.
      (OEIS A000058)
   
   def go(n):
       return 1 + reduce(
           lambda a, x: a * go(x),
           range(0, n),
           1
       ) if 0 != n else 2
   return map(go, count(0))


  1. ------------------------- TEST -------------------------
  2. 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)
   )


  1. MAIN ---

if __name__ == '__main__':

   main()</lang>
Output:
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

Raku

<lang perl6>my @S = {1 + [*] @S[^($++)]} … *;

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, $_ };</lang>

Output:
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

REXX

<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 numeric digits digits()-1 say 'sum of the first ' n " reciprocals using" digits() 'decimal digits: ' $ / 1</lang>

output   when using the default inputs:
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

Wren

Library: Wren-big

<lang ecmascript>import "/big" for BigInt, BigRat

var sylvester = [BigInt.two] var prod = BigInt.two var count = 1 while (true) {

   var next = prod + 1
   sylvester.add(next)
   count = count + 1
   if (count == 10) break
   prod = prod * next

} System.print("The first 10 terms in the Sylvester sequence are:") System.print(sylvester.join("\n"))

var sumRecip = sylvester.reduce(BigRat.zero) { |acc, s| acc + BigRat.new(1, s) } System.print("\nThe sum of their reciprocals as a rational number is:") System.print (sumRecip) System.print("\nThe sum of their reciprocals as a decimal number (to 211 places) is:") System.print(sumRecip.toDecimal(211))</lang>

Output:
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