

 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, ideally as an exact fraction.


Related tasks

 Egyptian fractions
 Harmonic series


See also
 
 OEIS A000058 - Sylvester's sequence


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
 