How to resolve the algorithm Juggler sequence step by step in the C++ programming language
How to resolve the algorithm Juggler sequence step by step in the C++ 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 C++ programming language
The provided C++ code explores the Juggler sequence, which is defined as follows:
Given a positive integer n
, the Juggler sequence starts with n
. In each subsequent step, if the current number is even, it is replaced by its square root, and if it is odd, it is replaced by the square root of its cube. The sequence continues until the number becomes 1.
The code defines a function called juggler
that takes a positive integer n
as input and returns a tuple containing the following information about the Juggler sequence starting with n
:
count
: The number of steps required to reach 1.max_count
: The maximum number of steps in the sequence.max
: The maximum value reached in the sequence.digits
: The number of digits in the maximum value reached in the sequence.
The main
function of the code demonstrates the use of the juggler
function by printing the results for a range of positive integers (n = 20
to n = 39
) and a set of specific large positive integers.
Here's a detailed explanation of the code:
-
Function
juggler
:- It takes a positive integer
n
as input. - It initializes three variables:
count
to 0 (to count the steps in the sequence).max_count
to 0 (to track the maximum number of steps in the sequence).a
ton
(to represent the current number in the sequence).max
ton
(to track the maximum value reached in the sequence).
- It enters a loop that continues until
a
becomes 1. - Inside the loop:
- If
a
is even, it updatesa
to its square root (a = sqrt(a)
). - If
a
is odd, it updatesa
to the square root of its cube (a = sqrt(big_int(a * a * a))
using GMP'sbig_int
class for large integer arithmetic). - It increments
count
by 1 to track the steps. - It checks if the current
a
is greater than the previously recorded maximummax
. If so, it updatesmax
andmax_count
to reflect the new maximum value and the number of steps it took to reach that maximum.
- If
- Finally, it returns a tuple containing
count
,max_count
,max
, and the number of digits inmax
.
- It takes a positive integer
-
Main Function (
main
):- It sets the output locale to enable locale-specific formatting.
- It prints the header line: "n l[n] i[n] h[n]" to indicate the columns in the output:
n
(number),l[n]
(steps to reach 1),i[n]
(maximum steps in the sequence), andh[n]
(maximum value reached). - It enters a loop to print the results for integers
n
from 20 to 39. - For each
n
, it calls thejuggler
function and unpacks the returned tuple into variables (count
,max_count
,max
, anddigits
). - It prints the formatted results.
- After the first loop, it prints a new header line: "n l[n] i[n] d[n]" to indicate the columns in the output:
n
(number),l[n]
(steps to reach 1),i[n]
(maximum steps in the sequence), andd[n]
(number of digits in the maximum value reached). - It enters a second loop to print the results for a set of predefined large integers (
n
). - For each
n
, it calls thejuggler
function and unpacks the returned tuple into variables (count
,max_count
,max
, anddigits
). - It prints the formatted results.
Source code in the cpp programming language
#include <cassert>
#include <iomanip>
#include <iostream>
#include <string>
#include <gmpxx.h>
using big_int = mpz_class;
auto juggler(int n) {
assert(n >= 1);
int count = 0, max_count = 0;
big_int a = n, max = n;
while (a != 1) {
if (a % 2 == 0)
a = sqrt(a);
else
a = sqrt(big_int(a * a * a));
++count;
if (a > max) {
max = a;
max_count = count;
}
}
return std::make_tuple(count, max_count, max, max.get_str().size());
}
int main() {
std::cout.imbue(std::locale(""));
std::cout << "n l[n] i[n] h[n]\n";
std::cout << "--------------------------------\n";
for (int n = 20; n < 40; ++n) {
auto [count, max_count, max, digits] = juggler(n);
std::cout << std::setw(2) << n << " " << std::setw(2) << count
<< " " << std::setw(2) << max_count << " " << max
<< '\n';
}
std::cout << '\n';
std::cout << " n l[n] i[n] d[n]\n";
std::cout << "----------------------------------------\n";
for (int n : {113, 173, 193, 2183, 11229, 15065, 15845, 30817, 48443,
275485, 1267909, 2264915, 5812827, 7110201, 56261531,
92502777, 172376627, 604398963}) {
auto [count, max_count, max, digits] = juggler(n);
std::cout << std::setw(11) << n << " " << std::setw(3) << count
<< " " << std::setw(3) << max_count << " " << digits
<< '\n';
}
}
You may also check:How to resolve the algorithm Rate counter step by step in the Raku programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the Fortran programming language
You may also check:How to resolve the algorithm Bernoulli numbers step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Input loop step by step in the LIL programming language
You may also check:How to resolve the algorithm Achilles numbers step by step in the Julia programming language