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: n_{k+1} =\begin{cases}\lfloor n_k^{1/2} \rfloor & \text{for even } n_k \\\lfloor n_k^{3/2} \rfloor & \text{for odd } n_k\end{cases}.

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.

Try It Yourself
redirect icon

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.

C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

// Recursive function to generate
// the Juggler sequence
void solve(long long n, vector<long long> &res)
{
    // Store current value
    res.push_back(n);

    // Base case: stop when n becomes 1
    if (n <= 1)
        return;

    // If n is even, take floor(sqrt(n))
    else if (n % 2 == 0)
    {
        n = floor(sqrt(n));
        solve(n, res);
    }
    // If n is odd, take floor(n * sqrt(n))
    else
    {
        n = floor(n * sqrt(n));
        solve(n, res);
    }
}

// Function to return the Juggler sequence
vector<long long> jugglerSequence(long long n)
{
    vector<long long> res;

    // Call recursive helper
    solve(n, res);

    return res;
}

// Driver code
int main()
{
    long long n = 9;

    // Get the Juggler sequence
    vector<long long> res = jugglerSequence(n);

    // Print the sequence
    for (auto x : res)
        cout << x << " ";

    return 0;
}
C
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

// Recursive function to generate
// the Juggler sequence
void solve(long long n, long long **res, int *size)
{
    // Store current value
    (*res) = realloc(*res, (*size + 1) * sizeof(long long));
    (*res)[(*size)++] = n;

    // Base case: stop when n becomes 1
    if (n <= 1)
        return;

    // If n is even, take floor(sqrt(n))
    else if (n % 2 == 0)
    {
        n = (long long)floor(sqrt(n));
        solve(n, res, size);
    }
    // If n is odd, take floor(n * sqrt(n))
    else
    {
        n = (long long)floor(n * sqrt(n));
        solve(n, res, size);
    }
}

// Function to return the Juggler sequence
long long* jugglerSequence(long long n, int *size)
{
    long long *res = (long long *)malloc(sizeof(long long));
    *size = 0;

    // Call recursive helper
    solve(n, &res, size);

    return res;
}

// Driver code
int main()
{
    long long n = 9;
    int size;

    // Get the Juggler sequence
    long long *res = jugglerSequence(n, &size);

    // Print the sequence
    for (int i = 0; i < size; i++)
        printf("%lld ", res[i]);

    free(res);
    return 0;
}
Java
import java.util.ArrayList;
import java.util.List;

public class GfG {

    // Recursive function to generate
    // the Juggler sequence
    private static void solve(long n, List<Long> res) {
        // Store current value
        res.add(n);

        // Base case: stop when n becomes 1
        if (n <= 1)
            return;

        // If n is even, take floor(sqrt(n))
        else if (n % 2 == 0) {
            n = (long) Math.floor(Math.sqrt(n));
            solve(n, res);
        }
        // If n is odd, take floor(n * sqrt(n))
        else {
            n = (long) Math.floor(n * Math.sqrt(n));
            solve(n, res);
        }
    }

    // Function to return the Juggler sequence
    public static List<Long> jugglerSequence(long n) {
        List<Long> res = new ArrayList<>();

        // Call recursive helper
        solve(n, res);

        return res;
    }

    // Driver code
    public static void main(String[] args) {
        long n = 9;

        // Get the Juggler sequence
        List<Long> res = jugglerSequence(n);

        // Print the sequence
        for (long x : res)
            System.out.print(x + " ");
    }
}
Python
import math

# Recursive function to generate
# the Juggler sequence
def solve(n, res):
    
    # Store current value
    res.append(n)

    # Base case: stop when n becomes 1
    if n <= 1:
        return

    # If n is even, take floor(sqrt(n))
    if n % 2 == 0:
        n = math.floor(math.sqrt(n))
        solve(n, res)
    
    # If n is odd, take floor(n * sqrt(n))
    else:
        n = math.floor(n * math.sqrt(n))
        solve(n, res)

# Function to return the Juggler sequence
def jugglerSequence(n):
    
    res = []

    # Call recursive helper
    solve(n, res)

    return res


# Driver code
if __name__ == "__main__":
    
    n = 9

    # Get the Juggler sequence
    res = jugglerSequence(n)

    # Print the sequence
    for x in res:
        print(x, end=" ")
C#
using System;
using System.Collections.Generic;

class GfG
{
    // Recursive function to generate
    // the Juggler sequence
    public void solve(long n, List<long> res)
    {
        // Store current value
        res.Add(n);

        // Base case: stop when n becomes 1
        if (n <= 1)
            return;

        // If n is even, take floor(sqrt(n))
        if (n % 2 == 0)
        {
            n = (long)Math.Floor(Math.Sqrt(n));
            solve(n, res);
        }
        // If n is odd, take floor(n * sqrt(n))
        else
        {
            n = (long)Math.Floor(n * Math.Sqrt(n));
            solve(n, res);
        }
    }

    // Function to return the Juggler sequence
    public List<long> jugglerSequence(long n)
    {
        List<long> res = new List<long>();

        // Call recursive helper
        solve(n, res);

        return res;
    }

    static void Main(string[] args)
    {
        long n = 9;

        GfG obj = new GfG();
        List<long> res = obj.jugglerSequence(n);

        foreach (long val in res)
        {
            Console.Write(val + " ");
        }
    }
}
JavaScript
'use strict';

// Recursive function to generate
// the Juggler sequence
function solve(n, res) {
    // Store current value
    res.push(n);

    // Base case: stop when n becomes 1
    if (n <= 1)
        return;

    // If n is even, take floor(sqrt(n))
    else if (n % 2 === 0) {
        n = Math.floor(Math.sqrt(n));
        solve(n, res);
    }
    // If n is odd, take floor(n * sqrt(n))
    else {
        n = Math.floor(n * Math.sqrt(n));
        solve(n, res);
    }
}

// Function to return the Juggler sequence
function jugglerSequence(n) {
    let res = [];

    // Call recursive helper
    solve(n, res);

    return res;
}

// Driver code
let n = 9;

// Get the Juggler sequence
let res = jugglerSequence(n);

// Print the sequence
console.log(res.join(' '));

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

C++
#include <bits/stdc++.h>
using namespace std;

// Function to generate the Juggler Sequence
vector<long long> jugglerSequence(long long n)
{
    // Initializing the result vector with the initial number
    vector<long long> res;
    res.emplace_back(n);

    // Loop until the number becomes 1
    while (n > 1)
    {
        // If the number is odd, perform the juggler equation
        if (n % 2)
            n = (sqrt(n) * n);
            
        // If the number is even, perform the juggler equation
        else
            n = sqrt(n);

        // Add the number to the result vector
        res.emplace_back(n);
    }

    // Return the final result vector
    return res;
}

// Driver Code
int main() {
    long long n = 9;

    vector<long long> ans = jugglerSequence(n);

    cout << "Juggler Sequence : ";
    for (auto x : ans) {
        cout << x << " ";
    }

    return 0;
}
C
#include <stdio.h>
#include <math.h>

// Function to generate the Juggler Sequence
void jugglerSequence(long long n, long long *res, int *len)
{
    // Initializing the result array with the initial number
    res[(*len)++] = n;

    // Loop until the number becomes 1
    while (n > 1)
    {
        // If the number is odd, perform the juggler equation
        if (n % 2)
            n = (sqrt(n) * n);
            
        // If the number is even, perform the juggler equation
        else
            n = sqrt(n);

        // Add the number to the result array
        res[(*len)++] = n;
    }
}

// Driver Code
int main() {
    long long n = 9;
    long long res[100];
    int len = 0;

    jugglerSequence(n, res, &len);

    printf("Juggler Sequence : ");
    for (int i = 0; i < len; i++) {
        printf("%lld ", res[i]);
    }

    return 0;
}
Java
import java.util.ArrayList;

// Function to generate the Juggler Sequence
public class GfG {
    public static ArrayList<Long> jugglerSequence(long n) {
        
        // Initializing the result array list with the initial number
        ArrayList<Long> res = new ArrayList<>();
        res.add(n);

        // Loop until the number becomes 1
        while (n > 1) {
            
            // If the number is odd, perform the juggler equation
            if (n % 2!= 0) {
                n = (long) (Math.sqrt(n) * n);
                
            // If the number is even, perform the juggler equation
            } else {
                n = (long) Math.sqrt(n);
            }

            // Add the number to the result array list
            res.add(n);
        }

        // Return the final result array list
        return res;
    }

    // Driver Code
    public static void main(String[] args) {
        long n = 9;

        ArrayList<Long> ans = jugglerSequence(n);

        System.out.print("Juggler Sequence : ");
        for (long x : ans) {
            System.out.print(x + " ");
        }
    }
}
Python
import math

# Function to generate the Juggler Sequence
def jugglerSequence(n):
    
    # Initializing the result list with the initial number
    res = [n]

    # Loop until the number becomes 1
    while n > 1:
        
        # If the number is odd, perform the juggler equation
        if n % 2:
            n = int(math.sqrt(n) * n)
            
        # If the number is even, perform the juggler equation
        else:
            n = int(math.sqrt(n))

        # Add the number to the result list
        res.append(n)

    # Return the final result list
    return res


# Driver Code
if __name__ == "__main__":
    n = 9

    ans = jugglerSequence(n)

    print("Juggler Sequence : ", end="")
    for x in ans:
        print(x, end=" ")
C#
using System;
using System.Collections.Generic;

class GfG
{
    // Function to generate the Juggler Sequence
    public List<long> jugglerSequence(long n)
    {
        // Initializing the result list with the initial number
        List<long> res = new List<long>();
        res.Add(n);

        // Loop until the number becomes 1
        while (n > 1)
        {
            // If the number is odd, perform the juggler equation
            if (n % 2 != 0)
                n = (long)(Math.Sqrt(n) * n);
                
            // If the number is even, perform the juggler equation
            else
                n = (long)Math.Sqrt(n);

            // Add the number to the result list
            res.Add(n);
        }

        // Return the final result list
        return res;
    }

    // Driver Code
    public static int Main()
    {
        long n = 9;

        GfG obj = new GfG();  
        List<long> ans = obj.jugglerSequence(n);

        Console.Write("Juggler Sequence : ");
        foreach (long x in ans)
        {
            Console.Write(x + " ");
        }

        return 0;
    }
}
JavaScript
// Function to generate the Juggler Sequence
function jugglerSequence(n) {
    
    // Initializing the result array with the initial number
    let res = [];
    res.push(n);

    // Loop until the number becomes 1
    while (n > 1) {
        
        // If the number is odd, perform the juggler equation
        if (n % 2 !== 0)
            n = Math.floor(Math.sqrt(n) * n);
            
        // If the number is even, perform the juggler equation
        else
            n = Math.floor(Math.sqrt(n));

        // Add the number to the result array
        res.push(n);
    }

    // Return the final result array
    return res;
}

// driver code
let n = 9;

let ans = jugglerSequence(n);

let output = "Juggler Sequence : ";
for (let x of ans) {
    output += x + " ";
}

console.log(output);

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