RosettaCodeData/Task/Sylvesters-sequence/Agena/sylvesters-sequence.agena
2026-04-30 12:34:36 -04:00

37 lines
1.4 KiB
Text

scope # calculate elements of Sylvester's Sequence
import mapm; # explicit import needed for: Linux, Mac OS X, Windows and Solaris
mapm.xdigits( 220 );
# returns a sequence set to the first n elements of Sylvester's Sequence
# starting from 2, the elements are the product of the previous elements plus 1
local constant b0, constant b1, constant b2 := mapm.xnumber( 0 ), mapm.xnumber( 1 ), mapm.xnumber( 2 );
local proc sylvester( n :: integer ) :: sequence
local result; create sequence result( n );
local product := b2;
result[ 1 ] := product;
for i from 2 to n do
result[ i ] := product + b1;
product *:= result[ i ]
od;
return result
end;
# returns a string representation of the integer portion of an xnumber
local constant asstring := proc( n :: xnumber ) :: string
local constant str := mapm.xtostring( n );
local constant point := "." in str;
return if point <> null then str[ 1 to point - 1 ] else str fi;
end;
scope # show the sequence and sum the reciprocals
local constant sseq := sylvester( 10 );
local reciprocalSum := b0;
for i from 1 to size sseq do
print( asstring( sseq[ i ] ) );
reciprocalSum +:= b1 / sseq[ i ]
od;
printf( "Sum of reciprocals: %s\n", mapm.xtostring( reciprocalSum ) )
epocs
epocs