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

58 lines
2.4 KiB
Text

scope # Jugglar sequences - starting with a[0] = n, a[k+1] = floor(sqrt(a[k])) if a[k] is even
# = floor(sqrt(a[k]))*a[k] otherwise
import mapm; # explicit import needed for: Linux, Mac OS X, Windows and Solaris
mapm.xdigits( 10_000 ); # enough digits for the first few stretch sequences
local constant b0, constant b1 := mapm.xnumber( 0 ), mapm.xnumber( 1 );
# for a[0] = a0, returns the number of terms required to reach a[n] = 1,
# the maximum value of the sequence before it reaches 1
# and the index at which the maximum was first reached
local proc jugglarStatistics( a0 :: number ) :: table
local ak, amax, aindex, mindex := mapm.xnumber( a0 ), b0, 0, 0;
while ak <> b1 do
if amax < ak then
amax, mindex := ak, aindex
fi;
aindex +:= 1;
local constant rootAk := mapm.xsqrt( ak );
ak := mapm.xfloor( if mapm.xisodd( ak ) then rootAk * ak else rootAk fi )
od;
return [ "a0" ~ a0, "length" ~ aindex, "max" ~ amax, "maxIndex" ~ mindex ]
end;
# returns a string representation of the integer portion of n if it is an xnumber,
# or n if it is a string or tostring( n ) otherwise
local proc bToIntegerString( n ) :: string
if typeof n = "string" then
return n
elif typeof n = "xnumber" then
local constant str := mapm.xtostring( n )
local constant point := "." in str
return if point <> null then str[ 1 to point - 1 ] else str fi
else
return tostring( n )
fi
end;
local proc printJugglarStatistics( jStats :: table )
local maxString := bToIntegerString( jStats.max );
if size maxString > 32 then
maxString := tostring( size maxString ) & " digits"
fi;
printf( "%6.0f%10.0f%10.0f %s\n", jStats.a0, jStats.length, jStats.maxIndex, maxString );
end;
scope
print( " n l[n] i[n] h[n]" );
print( "=====================================================================" );
for a0 from 20 to 39 do
printJugglarStatistics( jugglarStatistics( a0 ) )
od;
for a0 in seq( 113, 173, 193, 2183 ) do
printJugglarStatistics( jugglarStatistics( a0 ) )
od
end
end