Friday, October 10, 2025
HomeData Modelling & AIJuggler Sequence | Set 2 (Using Recursion)

Juggler Sequence | Set 2 (Using Recursion)

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

a_{k+1}=\begin{Bmatrix} \lfloor a_{k}^{1/2} \rfloor & for \quad even \quad a_k\\ \lfloor a_{k}^{3/2} \rfloor & for \quad odd \quad a_k \end{Bmatrix}
Juggler Sequence starting with number 3: 
5, 11, 36, 6, 2, 1
Juggler Sequence starting with number 9: 
9, 27, 140, 11, 36, 6, 2, 1

Given a number N, we have to print the Juggler Sequence for this number as the first term of the sequence. 

Examples

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

Input: N = 6 
Output: 6, 2, 1

Iterative approach: We have already seen the iterative approach in Set 1 of this problem.

Recursive approach: In this approach, we will recursively traverse starting from N. Follow the steps below for each recursive step

  • Output the value of N
  • If N has reached 1 end the recursion
  • Otherwise, follow the formula based on the number being odd or even and call the recursive function on the newly derived number.

Below is the implementation of the approach: 

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Recursive function to print
// the juggler sequence
void jum_sequence(int N)
{
 
    cout << N << " ";
 
    if (N <= 1)
        return;
    else if (N % 2 == 0)
    {
        N = floor(sqrt(N));
        jum_sequence(N);
    }
    else
    {
        N = floor(N * sqrt(N));
        jum_sequence(N);
    }
}
 
// Driver code
int main()
{
   
    // Juggler sequence starting with 10
    jum_sequence(10);
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java




// Java code for the above approach
class GFG
{
   
    // Recursive function to print
    // the juggler sequence
    public static void jum_sequence(int N) {
 
        System.out.print(N + " ");
 
        if (N <= 1)
            return;
        else if (N % 2 == 0) {
            N = (int) (Math.floor(Math.sqrt(N)));
            jum_sequence(N);
        } else {
            N = (int) Math.floor(N * Math.sqrt(N));
            jum_sequence(N);
        }
    }
 
    // Driver code
    public static void main(String args[]) {
 
        // Juggler sequence starting with 10
        jum_sequence(10);
    }
}
 
// This code is contributed by Saurabh Jaiswal


Python3




# Python code to implement the above approach
 
# Recursive function to print
# the juggler sequence
def jum_sequence(N):
     
    print(N, end =" ")
 
    if (N == 1):
        return
    elif N & 1 == 0:
        N = int(pow(N, 0.5))
        jum_sequence(N)
    else:
        N = int(pow(N, 1.5))
        jum_sequence(N)
 
 
# Juggler sequence starting with 10
jum_sequence(10)


C#




// C# code for the above approach
using System;
 
class GFG{
 
// Recursive function to print
// the juggler sequence
public static void jum_sequence(int N)
{
    Console.Write(N + " ");
 
    if (N <= 1)
        return;
    else if (N % 2 == 0)
    {
        N = (int)(Math.Floor(Math.Sqrt(N)));
        jum_sequence(N);
    }
    else
    {
        N = (int)Math.Floor(N * Math.Sqrt(N));
        jum_sequence(N);
    }
}
 
// Driver code
public static void Main()
{
     
    // Juggler sequence starting with 10
    jum_sequence(10);
}
}
 
// This code is contributed by Saurabh Jaiswal


Javascript




<script>
// Javascript code for the above approach
 
// Recursive function to print
// the juggler sequence
function jum_sequence(N){
 
    document.write(N +" ");
 
    if (N <= 1)
        return;
    else if (N % 2 == 0)
    {
        N = Math.floor(Math.sqrt(N));
        jum_sequence(N);
    }
    else
    {
        N = Math.floor(N * Math.sqrt(N));
        jum_sequence(N);
    }
}
 
// Driver code
// Juggler sequence starting with 10
 
jum_sequence(10);
     
// This code is contributed by gfgking
</script>


Output: 

10 3 5 11 36 6 2 1

 

Time Complexity: O(N) 
Auxiliary Space: O(1) 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS