Jump to content

Sylvester's sequence: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
(→‎{{header|Python}}: Added a functionally composed solution in Python.)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 307:
 
Sum of the reciprocals of first 10 elements: 0.9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999635</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds/displays the Sylvester's sequence and 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>
{{out|output|text=&nbsp; 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|Wren}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.