Juggler Sequence | Set 2 (Using Recursion)
Last Updated :
07 Dec, 2021
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)
Similar Reads
Golomb Sequence | Set 2 Given a number N. The task is to find the first N terms of the Golomb Sequence. Golomb sequence is a non-decreasing integer sequence where the n-th term is equal to the number of times n appears in the sequence. Input: N = 11 Output: 1 2 2 3 3 4 4 4 5 5 5 Explanation: The first term is 1. 1 appears
8 min read
Juggler Sequence 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
5 min read
String with additive sequence | Set-2 Given a string str, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. A valid string should contain at least three digits to make one addi
14 min read
Generating all possible Subsequences using Recursion including the empty one. Given an array arr[]. The task is to find all the possible subsequences of the given array using recursion.Examples: Input: arr[] = [1, 2, 3]Output : [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3], []Input: arr[] = [1, 2]Output : [2], [1], [1, 2], []Approach:For every element in the array, there a
5 min read
Longest Common Subsequence | DP using Memoization Given two strings s1 and s2, the task is to find the length of the longest common subsequence present in both of them. Examples: Input: s1 = âABCDGHâ, s2 = âAEDFHRâ Output: 3 LCS for input Sequences âAGGTABâ and âGXTXAYBâ is âGTABâ of length 4. Input: s1 = âstriverâ, s2 = ârajâ Output: 1 The naive s
13 min read