How to resolve the algorithm Juggler sequence step by step in the REXX programming language
How to resolve the algorithm Juggler sequence step by step in the REXX programming language
Table of Contents
Problem Statement
Background of the juggler sequence: Juggler sequences were publicized by an American mathematician and author Clifford A. Pickover. The name of the sequence gets it's name from the similarity of the rising and falling nature of the numbers in the sequences, much like balls in the hands of a juggler.
A juggler sequence is an integer sequence that starts with a positive integer a[0], with each subsequent term in the sequence being defined by the recurrence relation: If a juggler sequence reaches 1, then all subsequent terms are equal to 1. This is known to be the case for initial terms up to 1,000,000 but it is not known whether all juggler sequences after that will eventually reach 1.
Compute and show here the following statistics for juggler sequences with an initial term of a[n] where n is between 20 and 39 inclusive:
If your language supports big integers with an integer square root function, also compute and show here the same statistics for as many as you reasonably can of the following values for n: 113, 173, 193, 2183, 11229, 15065, 15845, 30817, 48443, 275485, 1267909, 2264915, 5812827 Those with fast languages and fast machines may also like to try their luck at n = 7110201. However, as h[n] for most of these numbers is thousands or millions of digits long, show instead of h[n]:
The results can be (partially) verified against the table here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Juggler sequence step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program calculates and displays the juggler sequence for any positive integer*/
numeric digits 20 /*define the number of decimal digits. */
parse arg LO HI list /*obtain optional arguments from the CL*/
if LO='' | LO="," then do; LO= 20; HI= 39; end /*Not specified? Then use the default.*/
if HI='' | HI="," then HI= LO /* " " " " " " */
w= length( commas(HI) ) /*find the max width of any number N. */
d= digits(); dd= d + d%3 + 1 /*get # numeric digits; another version*/
if LO>0 then say c('n',w) c("steps",7) c('maxIndex',8) c("biggest term" ,dd)
if LO>0 then say c('' ,w,.) c("" ,7,.) c('' ,8,.) c("" ,dd,.)
do n=LO to HI while n>0; steps= juggler(n) /*invoke JUGGLER: find the juggler num.*/
nc= commas(n) /*maybe add commas to N. */
say right(nc, w) c(steps, 8) c(imx, 8) right( commas(mx), dd-1)
end /*n*/
/*biggest term isn't shown for list #s.*/
xtra= words(list) /*determine how many numbers in list. */
if xtra==0 then exit 0 /*no special ginormous juggler numbers?*/
w= max(9, length( commas( word(list, xtra) ) ) ) /*find the max width of any list number*/
say; say; say
say c('n',w) c("steps",7) c('maxIndex',8) c("max number of digits",dd)
say c('' ,w,.) c("" ,7,.) c('' ,8,.) c("" ,dd,.)
do p=1 for xtra; n= word(list, p) /*process each of the numbers in list. */
steps= juggler(n); nc= commas(n) /*get a number & pass it on to JUGGLER.*/
say right(nc, w) c(steps, 8) c(imx, 8) right( commas(length(mx)), dd-1)
end /*p*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
c: parse arg c1,c2,c3; if c3=='' then c3= ' '; else c3= '─'; return center(c1,c2,c3)
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
Isqrt: procedure; parse arg x; q= 1; r= 0 /*obtain X; R will be the Isqrt(X). */
do until q>x; q= q * 4 /*multiply Q by four until > X. */
end /*until*/
do while q>1; q= q % 4 /*processing while Q>1; quarter Q. */
t= x - r - q; r= r % 2 /*set T ──► X-R-Q; halve R. */
if t>=0 then do; x= t; r= r + q /*T≥0? Then set X ──► T; add Q ──► R.*/
end
end /*while*/; return r /*return the integer square root of X.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
juggler: parse arg #; imx= 0; mx= # /*get N; set index of MX and MX ──► 0.*/
@.=0; @.1=1; @.3=1; @.5=1; @.7=1; @.9=1 /*set semaphores (≡1) for odd dec. digs*/
do j=1 until z==1 /*here is where we begin to juggle. */
parse var # '' -1 _ /*obtain the last decimal digit of #. */
if @._ then do; cube= #*#*# /*Odd? Then compute integer sqrt(#**3).*/
if pos(., cube)>0 then do /*need to increase decimal digits.*/
parse var cube with 'E' x
if x>=digits() then numeric digits x+2
end
z= Isqrt(#*#*#) /*compute the integer sqrt(#**3) */
end
else z= Isqrt(#) /*Even? Then compute integer sqrt(#). */
L= length(z)
if L>=d then numeric digits L+1 /*reduce the number of numeric digits. */
if z>mx then do; mx= z; imx= j; end /*found a new max; set MX; set IMX. */
#= z
end /*j*/; return j
You may also check:How to resolve the algorithm Floyd's triangle step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Matrix-exponentiation operator step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Own digits power sum step by step in the Nim programming language
You may also check:How to resolve the algorithm File size step by step in the Pike programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the C# programming language