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 :
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++ 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 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 |
# 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# 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 |
<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)