Generate Juggler Sequence in Java7 May 2025 | 6 min read Juggler SequenceIn number theory, Juggler Sequence consists of numbers beginning with a positive integer n, where each subsequent term is determined by whether the previous term is even or odd. The sequence continues until it reaches 1. How to find Juggler Sequence?Juggler sequence is defined recursively as follows:
We can also calculate the above sequence simply as follows:
n1/2 = sqrt(n) or √n
n3/2 = n1 * n1/2 or n * sqrt(n) or n*√n Let's understand it through an example. Example of Juggler SequenceLet's take an integer (n) 3 as first term of the sequence. Calculating the Second Term 3 is odd, so the next term can be calculated by using n1 * n1/2 3*√3 = 5.19 = 5 Calculating the Third Term 5 is odd, so the next term can be calculated by using n1 * n1/2 5*√5 = 11.18 = 11 Calculating the Fourth Term 11 is odd, so the next term can be calculated by using n1 * n1/2 11*√11 = 36.48 = 36 Calculating the Fifth Term 36 is even, so the next term can be calculated by using n1/2 or √n √36 = 6 Calculating the Sixth Term 6 is even, so the next term can be calculated by using n1/2 or √n √6 = 2.44 = 2 Calculating the Seventh Term 2 is even, so the next term can be calculated by using n1/2 or √n √2 = 1.41 = 1 Hence, the sequence is: 3, 5, 11, 36, 6, 2, 1 Let's see some other juggler sequences. Input: 10 Output: 10, 3, 5, 11, 36, 6, 2, 1 Input: 7 Output: 7, 18, 4, 2, 1 Iterative ApproachThe iterative approach uses loops to repeatedly execute a set of instructions until a condition is met. It is commonly used for problems that require sequential calculations or repetitive tasks without recursion. AlgorithmStep 1: Start with a positive integer n as the initial term of the Juggler Sequence. Step 2: Print the value of n as the first term of the sequence. Step 3: Repeat the following steps until n becomes 1:
Step 4: Continue repeating the calculations and printing each term until n becomes 1. Step 5: End the process once the sequence reaches the number 1, as it marks the termination of the Juggler Sequence. Let's implement the above steps in a Java program. File Name: JugglerSequence.java Output: Juggler Sequence for 10: 10 3 5 11 36 6 2 1 Time Complexity: O(log N) The value of n reduces significantly in each iteration, leading to logarithmic time complexity. Auxiliary Space: O(1) The algorithm uses a constant amount of extra space for calculations. Using RecursionThe approach uses recursion to generate the Juggler Sequence by repeatedly applying mathematical operations based on whether the current number is even or odd. Each recursive call calculates the next term in the sequence until the base case (n = 1) is reached. AlgorithmStep 1: Start with a predefined integer n and Initialize n to begin the Juggler Sequence. Step 2: Print the current value of n. Display the value of n as part of the sequence. Step 3: If n is 1, stop the recursion, print it and exit the recursion. Step 4: If n is even, compute floor(sqrt(n)) and call recursively. If n is even, calculate sqrt(n) rounded down and recursively call with the new value. Step 5: If n is odd, compute floor(n^1.5) and call recursively. If n is odd, calculate n^1.5 rounded down and recursively call with the new value. Let's implement the above steps in a Java program. File Name: JugglerSequence.java Output: Juggler Sequence for 15: 15 58 7 18 4 2 1 Time Complexity: O(log n), due to the recursive reduction of n in each step. Auxiliary Space Complexity: O(log n), due to the recursion stack. Next TopicflatMap() Method in Java 8 |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India