Sylvester's sequence: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 28:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F sylverster(lim)
V result = [BigInt(2)]
L 2..lim
Line 42:
L(item) l
s += 1 / Float(item)
print("\nSum of the reciprocals of the first 10 terms: #.17".format(s))</langsyntaxhighlight>
 
{{out}}
Line 64:
{{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 reciprocals in the output has been manually edited to replace a large number of nines with ... to reduce the width.
<langsyntaxhighlight 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 #
Line 90:
print( ( "Sum of reciprocals: ", reciprocal sum, newline ) )
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 108:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">sylvester: function [lim][
result: new [2]
loop 2..lim 'x [
Line 124:
 
print "Sum of the reciprocals of the first 10 items:"
print sumRep</langsyntaxhighlight>
 
{{out}}
Line 134:
1.0</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK --bignum -f SYLVESTERS_SEQUENCE.AWK
BEGIN {
Line 147:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 169:
{{works with|Chipmunk Basic}}
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">
PRINT "10 primeros términos de la sucesión de sylvester:"
PRINT
Line 187:
PRINT "suma de sus recíprocos: "; suma
END
</syntaxhighlight>
</lang>
 
 
==={{header|FreeBASIC}}===
'''precisión estándar'''
<langsyntaxhighlight lang="freebasic">
Dim As Double sylvester, suma = 0
 
Line 205:
Print !"\nSuma de sus rec¡procos:"; suma
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 225:
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">
<lang PureBasic>
OpenConsole()
PrintN("10 primeros términos de la sucesión de Sylvester:")
Line 244:
CloseConsole()
End
</syntaxhighlight>
</lang>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">
print "10 primeros términos de la sucesión de Sylvester:"
 
Line 260:
print "\nSuma de sus rec¡procos: ", suma
end
</syntaxhighlight>
</lang>
 
 
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <boost/rational.hpp>
Line 287:
}
std::cout << "Sum of reciprocals: " << sum << '\n';
}</langsyntaxhighlight>
 
{{out}}
Line 306:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight 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>
</lang>
{{out}}
<pre>
Line 334:
 
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: io kernel lists lists.lazy math prettyprint ;
 
: lsylvester ( -- list ) 2 [ dup sq swap - 1 + ] lfrom-by ;
Line 342:
 
"Sum of the reciprocals of first 10 elements:" print
0 [ recip + ] foldl .</langsyntaxhighlight>
{{out}}
<pre>
Line 364:
 
=={{header|Fermat}}==
<langsyntaxhighlight 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;</langsyntaxhighlight>
{{out}}<pre>
syl[1] := 2
Line 391:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 424:
fmt.Println("\nThe sum of their reciprocals as a decimal number (to 211 places) is:")
fmt.Println(sumRecip.FloatString(211))
}</langsyntaxhighlight>
 
{{out}}
Line 448:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">sylvester :: [Integer]
sylvester = map s [0 ..]
where
Line 463:
 
putStr "Sum of reciprocals by fold: "
print $ foldr ((+) . (1 /) . fromInteger) 0 $ take 10 sylvester</langsyntaxhighlight>
{{out}}
<pre>First 10 elements of Sylvester's sequence:
Line 481:
 
Simpler way of generating sequence:
<langsyntaxhighlight lang="haskell">sylvester :: [Integer]
sylvester = iterate (\x -> x * (x-1) + 1) 2</langsyntaxhighlight>
 
or applicatively:
 
<langsyntaxhighlight lang="haskell">sylvester :: [Integer]
sylvester = iterate (succ . ((*) <*> pred)) 2</langsyntaxhighlight>
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq"># Generate the sylvester integers:
def sylvester:
foreach range(0; infinite) as $i ({prev: 1, product: 1};
.product *= .prev
| .prev = .product + 1;
.prev);</langsyntaxhighlight>
Left padding:
<syntaxhighlight lang="jq">
<lang jq>
def lpad($len; $fill): tostring | ($len - length) as $l | ($fill * $l)[:$l] + .;
def lpad($len): lpad($len; " ");
def lpad: lpad(4);</langsyntaxhighlight>
The task:
<langsyntaxhighlight 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>
</lang>
{{out}}
For integer precision, we will use `gojq`, the "go" implementation of jq.
Line 525:
=={{header|Julia}}==
 
<langsyntaxhighlight 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))
</langsyntaxhighlight>{{out}}
<pre>
1 => 2
Line 547:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Rest[Nest[Append[#, (Times @@ #) + 1] &, {1}, 10]]
N[Total[1/%], 250]</langsyntaxhighlight>
{{out}}
<pre>{2,3,7,43,1807,3263443,10650056950807,113423713055421844361000443,12864938683278671740537145998360961546653259485195807,165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443}
Line 555:
=={{header|Nim}}==
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import sequtils
import bignum
 
Line 569:
var sum = newRat()
for item in list: sum += newRat(1, item)
echo "\nSum of the reciprocals of the first 10 terms: ", sum.toFloat</langsyntaxhighlight>
 
{{out}}
Line 587:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight 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]))</langsyntaxhighlight>
{{out}}<pre>[2, 3, 7, 43, 1807, 3263443, 10650056950807, 113423713055421844361000443, 12864938683278671740537145998360961546653259485195807, 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443]
 
Line 600:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 613:
 
say "First 10 elements of Sylvester's sequence: @S";
say "\nSum of the reciprocals of first 10 elements: " . float $sum;</langsyntaxhighlight>
{{out}}
<pre>First 10 elements of Sylvester's sequence: 2 3 7 43 1807 3263443 10650056950807 113423713055421844361000443 12864938683278671740537145998360961546653259485195807 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Line 621:
=={{header|Phix}}==
=== standard precision ===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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 630:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 647:
=== 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).
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (mpfr_set_default_prec[ision] has been renamed)
Line 667:
<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;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">211</span><span style="color: #0000FF;">))})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 687:
<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).
<langsyntaxhighlight lang="plm">100H: /* CALCULATE ELEMENTS OF SYLVESTOR'S SEQUENCE */
 
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
Line 807:
CALL PRINT$LONG$INTEGER( .PRODUCT );
CALL PRINT$NL;
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 824:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">sylvesters_sequence(N, S, R):-
sylvesters_sequence(N, S, 2, R, 0).
 
Line 842:
N is numerator(Sum),
D is denominator(Sum),
writef('\nSum of reciprocals: %t / %t\n', [N, D]).</langsyntaxhighlight>
 
{{out}}
Line 863:
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">'''Sylvester's sequence'''
 
from functools import reduce
Line 904:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 10 terms of OEIS A000058:
Line 923:
 
Or as an iteration:
<langsyntaxhighlight lang="python">'''Sylvester's sequence'''
 
from functools import reduce
Line 970:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 10 terms of OEIS A000058:
Line 989:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery">[ $ "bigrat.qky" loadfile ] now!
 
' [ 2 ] 9 times [ dup -1 peek dup 2 ** swap - 1+ join ]
Line 995:
dup witheach [ echo cr ] cr
 
0 n->v rot witheach [ n->v 1/v v+ ] 222 point$ echo$</langsyntaxhighlight>
 
{{out}}
Line 1,014:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>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, $_ };</langsyntaxhighlight>
{{out}}
<pre>First 10 elements of Sylvester's sequence: 2 3 7 43 1807 3263443 10650056950807 113423713055421844361000443 12864938683278671740537145998360961546653259485195807 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Line 1,025:
 
=={{header|REXX}}==
<langsyntaxhighlight 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.*/
Line 1,038:
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,056:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def sylvester(n) = (1..n).reduce(2){|a| a*a - a + 1 }
(0..9).each {|n| puts "#{n}: #{sylvester n}" }
Line 1,062:
Sum of reciprocals of first 10 terms:
#{(0..9).sum{|n| 1.0r / sylvester(n)}.to_f }"
</syntaxhighlight>
</lang>
{{out}}
<pre>0: 2
Line 1,079:
</pre>
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define sylvester
(lambda (x)
(if (= x 1)
Line 1,087:
(print list)
(newline)
(print (apply + (map / list)))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,094:
</pre>
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigint.s7i";
include "bigrat.s7i";
Line 1,115:
writeln("\nSum of the reciprocals of the first 10 elements:");
writeln(reciprocalSum digits 210);
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,135:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func sylvester_sequence(n) {
1..n -> reduce({|a| a*(a-1) + 1 }, 2)
}
Line 1,143:
 
say "\nSum of reciprocals of first 10 terms: "
say 10.of(sylvester_sequence).sum {|n| 1/n }.as_dec(230)</langsyntaxhighlight>
 
{{out}}
Line 1,167:
Using mkrd's BigNumber library.
 
<langsyntaxhighlight lang="swift">import BigNumber
 
func sylvester(n: Int) -> BInt {
Line 1,187:
}
 
print("Sum of the reciprocals of first ten in sequence: \(sum)")</langsyntaxhighlight>
 
{{out}}
Line 1,204:
 
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">
<lang Verilog>
module main;
integer i;
Line 1,228:
end
endmodule
</syntaxhighlight>
</lang>
{{out}}
<pre>10 primeros términos de la sucesión de sylvester:
Line 1,248:
=={{header|Wren}}==
{{libheader|Wren-big}}
<langsyntaxhighlight lang="ecmascript">import "/big" for BigInt, BigRat
 
var sylvester = [BigInt.two]
Line 1,267:
System.print (sumRecip)
System.print("\nThe sum of their reciprocals as a decimal number (to 211 places) is:")
System.print(sumRecip.toDecimal(211))</langsyntaxhighlight>
 
{{out}}
10,333

edits