Sylvester's sequence: Difference between revisions

Added Algol 68
No edit summary
(Added Algol 68)
Line 22:
;* [[Harmonic series]]
<br/>
 
=={{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 reciprocols 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 reciprocols #
LONG LONG REAL reciprocol sum := 0;
FOR i FROM LWB seq TO UPB seq DO
print( ( whole( seq[ i ], 0 ), newline ) );
reciprocol sum +:= 1 / seq[ i ]
OD;
print( ( "Sum of reciprocols: ", reciprocol sum, newline ) )
END
</lang>
{{out}}
<pre>
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of reciprocols: +9.99999999999999999999999999999999999999999...999999999999999999999999999964e -1
</pre>
 
=={{header|Factor}}==
3,048

edits