VOOZH about

URL: https://www.geeksforgeeks.org/dsa/juggler-sequence/

⇱ Juggler Sequence - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Juggler Sequence

Last Updated : 12 May, 2026

Juggler Sequence is a series of integers in which the first term starts with a positive integer number n and the remaining terms are generated from the immediate previous term using the below recurrence relation: .

Note:

  • The terms in Juggler Sequence first increase to a peak value and then start decreasing.
  • The last term in Juggler Sequence is always 1.

Given a number n, find the Juggler Sequence for this number as the first term of the sequence until it becomes 1.

Examples:

Input: n = 9
Output: 9 27 140 11 36 6 2 1
Explanation: We start with 9 and use the above formula to get next terms.

Input: n = 6
Output: 6 2 1
Explanation: [61/2] = 2, [21/2] = 1.

Recursive Approach

The idea is to generate the Juggler sequence starting from n using recursion. At each step, print n, and if n == 1, stop. We compute the next term based on parity: if n is even, take √n, else take n × √n, and recursively repeat until 1 is reached.


Output
9 27 140 11 36 6 2 1 

Time Complexity: O(k), Where k is the number of terms in the Juggler sequence.
Auxiliary Space : O(k)

Iterative Approach

The idea is to simulate the Juggler sequence by iteratively applying the given rules. Starting from n, if it is odd, compute n * √n, otherwise compute √n, and continue until n becomes 1 while storing each value in a vector.

Let us understand with an example:
Input: n = 9
Start: res = [9]

  • n = 9 (odd) -> 3 * 9 = 27 -> [9, 27]
  • n = 27 (odd) -> 5.196 * 27 ≈ 140 -> [9, 27, 140]
  • n = 140 (even) -> √140 ≈ 11 -> [9, 27, 140, 11]
  • n = 11 (odd) -> 3.316 * 11 ≈ 36 -> [9, 27, 140, 11, 36]
  • n = 36 (even) -> 6 -> [9, 27, 140, 11, 36, 6]
  • n = 6 (even) -> 2 -> [9, 27, 140, 11, 36, 6, 2]
  • n = 2 (even) -> 1 -> [9, 27, 140, 11, 36, 6, 2, 1]

Stop at n = 1, Output: 9 27 140 11 36 6 2 1


Output
Juggler Sequence : 9 27 140 11 36 6 2 1 

Time Complexity: O(k), Where k is the number of terms in the Juggler sequence.
Auxiliary Space : O(1)

Comment
Article Tags:
Article Tags: