Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Sylvester's_sequence

View file

@ -0,0 +1,22 @@
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|OEIS A000058 - Sylvester's sequence]]
<br>

View file

@ -0,0 +1,15 @@
F sylverster(lim)
V result = [BigInt(2)]
L 2..lim
result.append(product(result) + 1)
R result
V l = sylverster(10)
print(First 10 terms of the Sylvester sequence:)
L(item) l
print(item)
V s = 0.0
L(item) l
s += 1 / Float(item)
print("\nSum of the reciprocals of the first 10 terms: #.17".format(s))

View file

@ -0,0 +1,26 @@
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

View file

@ -0,0 +1,12 @@
# syntax: GAWK --bignum -f SYLVESTERS_SEQUENCE.AWK
BEGIN {
start = 1
stop = 10
for (i=start; i<=stop; i++) {
sylvester = (i == 1) ? 2 : sylvester*sylvester-sylvester+1
printf("%2d: %d\n",i,sylvester)
sum += 1 / sylvester
}
printf("\nSylvester sequence %d-%d: sum of reciprocals %30.28f\n",start,stop,sum)
exit(0)
}

View file

@ -0,0 +1,17 @@
sylvester: function [lim][
result: new [2]
loop 2..lim 'x [
'result ++ inc fold result .seed:1 [a b][a * b]
]
return result
]
lst: sylvester 10
print "First 10 terms of the Sylvester sequence:"
print lst
print ""
sumRep: round sum map lst => [1 // &]
print "Sum of the reciprocals of the first 10 items:"
print sumRep

View file

@ -0,0 +1,17 @@
PRINT "10 primeros términos de la sucesión de sylvester:"
PRINT
LET suma = 0
FOR i = 1 to 10
IF i = 1 then
LET sylvester = 2
ELSE
LET sylvester = sylvester*sylvester-sylvester+1
END IF
PRINT i; ": "; sylvester
LET suma = suma + 1 / sylvester
NEXT i
PRINT
PRINT "suma de sus recíprocos: "; suma
END

View file

@ -0,0 +1,23 @@
#include <iomanip>
#include <iostream>
#include <boost/rational.hpp>
#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';
}

View file

@ -0,0 +1,4 @@
// 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

View file

@ -0,0 +1,9 @@
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 .

View file

@ -0,0 +1,6 @@
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;

View file

@ -0,0 +1,12 @@
Dim As Double sylvester, suma = 0
Print "10 primeros trminos de la sucesi¢n de Sylvester:"
For i As Byte = 1 To 10
sylvester = Iif(i=1, 2, sylvester*sylvester-sylvester+1)
Print Using "##: &"; i; sylvester
suma += 1 / sylvester
Next i
Print !"\nSuma de sus rec¡procos:"; suma
Sleep

View file

@ -0,0 +1,34 @@
package main
import (
"fmt"
"math/big"
)
func main() {
one := big.NewInt(1)
two := big.NewInt(2)
next := new(big.Int)
sylvester := []*big.Int{two}
prod := new(big.Int).Set(two)
count := 1
for count < 10 {
next.Add(prod, one)
sylvester = append(sylvester, new(big.Int).Set(next))
count++
prod.Mul(prod, next)
}
fmt.Println("The first 10 terms in the Sylvester sequence are:")
for i := 0; i < 10; i++ {
fmt.Println(sylvester[i])
}
sumRecip := new(big.Rat)
for _, s := range sylvester {
sumRecip.Add(sumRecip, new(big.Rat).SetFrac(one, s))
}
fmt.Println("\nThe sum of their reciprocals as a rational number is:")
fmt.Println(sumRecip)
fmt.Println("\nThe sum of their reciprocals as a decimal number (to 211 places) is:")
fmt.Println(sumRecip.FloatString(211))
}

View file

@ -0,0 +1,16 @@
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

View file

@ -0,0 +1,2 @@
sylvester :: [Integer]
sylvester = iterate (\x -> x * (x-1) + 1) 2

View file

@ -0,0 +1,2 @@
sylvester :: [Integer]
sylvester = iterate (succ . ((*) <*> pred)) 2

View file

@ -0,0 +1,6 @@
# Generate the sylvester integers:
def sylvester:
foreach range(0; infinite) as $i ({prev: 1, product: 1};
.product *= .prev
| .prev = .product + 1;
.prev);

View file

@ -0,0 +1,3 @@
def lpad($len; $fill): tostring | ($len - length) as $l | ($fill * $l)[:$l] + .;
def lpad($len): lpad($len; " ");
def lpad: lpad(4);

View file

@ -0,0 +1,5 @@
[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)"

View file

@ -0,0 +1,5 @@
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))

View file

@ -0,0 +1,2 @@
Rest[Nest[Append[#, (Times @@ #) + 1] &, {1}, 10]]
N[Total[1/%], 250]

View file

@ -0,0 +1,15 @@
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

View file

@ -0,0 +1,5 @@
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]))

View file

@ -0,0 +1,121 @@
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

View file

@ -0,0 +1,14 @@
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;

View file

@ -0,0 +1,10 @@
(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>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #000000;">2</span><span style="color: #0000FF;">:</span><span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</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: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"%d: %d\n"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"%d: %g\n"</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</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;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">n</span>
<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>
<!--

View file

@ -0,0 +1,21 @@
(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)
-- (and mpfr_sprintf() replaced with mpfr_get_fixed())</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">nm1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(</span><span style="color: #000000;">720</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">rn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">mpz_sub_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nm1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nm1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_add_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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;">"%d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">mpfr_set_z</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_si_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span>
<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>
<!--

View file

@ -0,0 +1,19 @@
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]).

View file

@ -0,0 +1,18 @@
OpenConsole()
PrintN("10 primeros términos de la sucesión de Sylvester:")
suma.d = 0
For i.i = 1 To 10
If i = 1
sylvester.d = 2
Else
sylvester.d = sylvester*sylvester-sylvester+1
EndIf
PrintN(Str(i) + ": " + StrD(sylvester))
suma = suma + 1 / sylvester
Next i
Print(#CRLF$ + "Suma de sus recíprocos: " + StrD(suma))
Input()
CloseConsole()
End

View file

@ -0,0 +1,42 @@
'''Sylvester's sequence'''
from functools import reduce
from itertools import count, islice
# 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))
# ------------------------- TEST -------------------------
# 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)
)
# MAIN ---
if __name__ == '__main__':
main()

View file

@ -0,0 +1,48 @@
'''Sylvester's sequence'''
from functools import reduce
from itertools import islice
# sylvester :: [Int]
def sylvester():
'''A non finite sequence of the terms of OEIS A000058
'''
return iterate(
lambda x: x * (x - 1) + 1
)(2)
# ------------------------- TEST -------------------------
# 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)
)
# ----------------------- GENERIC ------------------------
# iterate :: (a -> a) -> a -> Gen [a]
def iterate(f):
'''An infinite list of repeated
applications of f to x.
'''
def go(x):
v = x
while True:
yield v
v = f(v)
return go
# MAIN ---
if __name__ == '__main__':
main()

View file

@ -0,0 +1,7 @@
[ $ "bigrat.qky" loadfile ] now!
' [ 2 ] 9 times [ dup -1 peek dup 2 ** swap - 1+ join ]
dup witheach [ echo cr ] cr
0 n->v rot witheach [ n->v 1/v v+ ] 222 point$ echo$

View file

@ -0,0 +1,14 @@
/*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 /*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

View file

@ -0,0 +1,5 @@
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, $_ };

View file

@ -0,0 +1,6 @@
def sylvester(n) = (1..n).reduce(2){|a| a*a - a + 1 }
(0..9).each {|n| puts "#{n}: #{sylvester n}" }
puts "
Sum of reciprocals of first 10 terms:
#{(0..9).sum{|n| 1.0r / sylvester(n)}.to_f }"

View file

@ -0,0 +1,9 @@
(define sylvester
(lambda (x)
(if (= x 1)
2
(let ((n (sylvester (- x 1)))) (- (* n n) n -1)))))
(define list (map sylvester '(1 2 3 4 5 6 7 8 9 10)))
(print list)
(newline)
(print (apply + (map / list)))

View file

@ -0,0 +1,22 @@
$ include "seed7_05.s7i";
include "bigint.s7i";
include "bigrat.s7i";
const func bigInteger: nextSylvester (in bigInteger: prev) is
return prev * prev - prev + 1_;
const proc: main is func
local
var bigInteger: number is 2_;
var bigRational: reciprocalSum is 0_ / 1_;
var integer: n is 0;
begin
writeln("First 10 elements of Sylvester's sequence:");
for n range 1 to 10 do
writeln(number);
reciprocalSum +:= 1_ / number;
number := nextSylvester(number);
end for;
writeln("\nSum of the reciprocals of the first 10 elements:");
writeln(reciprocalSum digits 210);
end func;

View file

@ -0,0 +1,9 @@
func sylvester_sequence(n) {
1..n -> reduce({|a| a*(a-1) + 1 }, 2)
}
say "First 10 terms in Sylvester's sequence:"
10.of(sylvester_sequence).each_kv{|k,v| '%2s: %s' % (k,v) -> say }
say "\nSum of reciprocals of first 10 terms: "
say 10.of(sylvester_sequence).sum {|n| 1/n }.as_dec(230)

View file

@ -0,0 +1,21 @@
import BigNumber
func sylvester(n: Int) -> BInt {
var a = BInt(2)
for _ in 0..<n {
a = a * a - a + 1
}
return a
}
var sum = BDouble(0)
for n in 0..<10 {
let syl = sylvester(n: n)
sum += BDouble(1) / BDouble(syl)
print(syl)
}
print("Sum of the reciprocals of first ten in sequence: \(sum)")

View file

@ -0,0 +1,23 @@
module main;
integer i;
real suma, num;
initial begin
$display("10 primeros términos de la sucesión de sylvester:");
$display("");
suma = 0;
num = 0;
for(i=1; i<=10; i=i+1) begin
if (i==1) num = 2;
else num = num * num - num + 1;
$display(i, ": ", num);
suma = suma + 1 / num;
end
$display("");
$display("suma de sus recíprocos: ", suma);
$finish ;
end
endmodule

View file

@ -0,0 +1,20 @@
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))

View file

@ -0,0 +1,11 @@
print "10 primeros términos de la sucesión de Sylvester:"
suma = 0
for i = 1 to 10
if i=1 then sylvester = 2 else sylvester = sylvester*sylvester-sylvester+1 : fi
print i using("##"), ": ", sylvester
suma = suma + 1 / sylvester
next i
print "\nSuma de sus rec¡procos: ", suma
end