How to resolve the algorithm Juggler sequence step by step in the jq programming language
How to resolve the algorithm Juggler sequence step by step in the jq 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 jq programming language
Source code in the jq programming language
def juggler:
. as $n
| if $n < 1 then "juggler starting value must be a positive integer." | error
else { a: $n, count: 0, maxCount: 0, max: $n }
| until (.a == 1;
if .a % 2 == 0 then .a |= isqrt
else .a = ((.a * .a * .a)|isqrt)
end
| .count += 1
| if .a > .max
then .max = .a
| .maxCount = .count
else .
end)
| [.count, .maxCount, .max, (.max|tostring|length)]
end
;
def fmt(a;b;c;d):
"\(.[0]|lpad(a)) \(.[1]|lpad(b)) \(.[2]|lpad(c)) \(.[3]|lpad(d))" ;
def task1:
"n l[n] i[n] h[n]",
"-----------------------------------",
(range(20; 40)
| . as $n
| juggler as $res
| [$n, $res[0], $res[1], $res[2] ]
| fmt(4;4;4;14) ) ;
def task2:
def nums:[113, 173, 193, 2183, 11229, 15065, 15845, 30817];
" n l[n] i[n] d[n]",
"-----------------------------",
(nums[]
| . as $n
| juggler as $res
| [$n, $res[0], $res[1], $res[3] ]
| fmt(6; 6; 6; 8) );
task1, "", task2
You may also check:How to resolve the algorithm Sorting algorithms/Patience sort step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the REXX programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Frink programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the OCaml programming language
You may also check:How to resolve the algorithm Sum and product puzzle step by step in the Kotlin programming language