How to resolve the algorithm Juggler sequence step by step in the Python programming language
How to resolve the algorithm Juggler sequence step by step in the Python 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 Python programming language
The snippet of code provided is a Python program that generates a sequence of numbers known as the Juggler series for a given starting number k
. The Juggler series is defined as follows: if k
is even, the next number in the series is k
divided by 2; if k
is odd, the next number is k
multiplied by 3 and then 1 is added.
The program uses the math.isqrt()
function to calculate the integer square root of a number, and iterates over the Juggler series for a maximum of maxiters
iterations, or until the series converges to 1.
For each starting number k
, the program prints the value of k
, the length of the Juggler series starting with k
(l(n)
), the iteration at which the series reaches its maximum value (i(n)
), and the maximum value in the series (h(n)
).
Here is a breakdown of the code:
-
The program imports the
isqrt()
function from themath
module. -
The
juggler()
function takes three arguments:k
(the starting number),countdig
(a boolean value indicating whether to count the number of digits in the maximum value), andmaxiters
(the maximum number of iterations). -
The function initializes three variables:
m
(the current number in the Juggler series),maxj
(the maximum value in the series), andmaxjpos
(the iteration at which the maximum value occurs). -
The function enters a loop that iterates over the Juggler series for a maximum of
maxiters
iterations. -
In each iteration, the function checks if
m
is even. If it is, the function dividesm
by 2. Ifm
is odd, the function multipliesm
by 3 and then adds 1. -
The function checks if
m
is greater than or equal tomaxj
. If it is, the function updatesmaxj
andmaxjpos
to the new values. -
The function checks if
m
is equal to 1. If it is, the function prints the values ofk
,i
,maxjpos
, andmaxj
(or the number of digits inmaxj
ifcountdig
is True). The function then returns the value ofi
. -
If the loop completes without
m
reaching 1, the function prints an error message. -
The main program prints a header line and then calls the
juggler()
function for each starting number in the range 20 to 40, and for each starting number in the list [113, 173, 193, 2183, 11229, 15065, 15845, 30817, 48443, 275485, 1267909].
Source code in the python programming language
from math import isqrt
def juggler(k, countdig=True, maxiters=1000):
m, maxj, maxjpos = k, k, 0
for i in range(1, maxiters):
m = isqrt(m) if m % 2 == 0 else isqrt(m * m * m)
if m >= maxj:
maxj, maxjpos = m, i
if m == 1:
print(f"{k: 9}{i: 6,}{maxjpos: 6}{len(str(maxj)) if countdig else maxj: 20,}{' digits' if countdig else ''}")
return i
print("ERROR: Juggler series starting with $k did not converge in $maxiters iterations")
print(" n l(n) i(n) h(n) or d(n)\n-------------------------------------------")
for k in range(20, 40):
juggler(k, False)
for k in [113, 173, 193, 2183, 11229, 15065, 15845, 30817, 48443, 275485, 1267909]:
juggler(k)
You may also check:How to resolve the algorithm Call a function step by step in the D programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the Haskell programming language
You may also check:How to resolve the algorithm Egyptian division step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Pragmatic directives step by step in the REXX programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the PureBasic programming language