Juggler Sequence
Last Updated :
11 Sep, 2023
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:
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: 9
Output: 9, 27, 140, 11, 36, 6, 2, 1
We start with 9 and use above formula to get
next terms.
Input: 6
Output: 6, 2, 1
C++
#include <bits/stdc++.h>
using namespace std;
void printJuggler( long long n)
{
long long a = n;
cout << a << " " ;
while (a != 1)
{
long long b = 0;
if (a % 2 == 0)
b = floor ( sqrt (a));
else
b = floor ( sqrt (a) *
sqrt (a) * sqrt (a));
cout << b << " " ;
a = b;
}
}
int main()
{
printJuggler(37);
cout << "\n" ;
printJuggler(9);
return 0;
}
|
C
#include<stdio.h>
#include<math.h>
void printJuggler( int n)
{
int a = n;
printf ( "%d " , a);
while (a != 1)
{
int b = 0;
if (a%2 == 0)
b = floor ( sqrt (a));
else
b = floor ( sqrt (a)* sqrt (a)* sqrt (a));
printf ( "%d " , b);
a = b;
}
}
int main()
{
printJuggler(3);
printf ( "\n" );
printJuggler(9);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG {
static void printJuggler( int n)
{
int a = n;
System.out.print(a+ " " );
while (a != 1 )
{
int b = 0 ;
if (a% 2 == 0 )
b = ( int )Math.floor(Math.sqrt(a));
else
b =( int ) Math.floor(Math.sqrt(a) *
Math.sqrt(a) * Math.sqrt(a));
System.out.print( b+ " " );
a = b;
}
}
public static void main (String[] args) {
printJuggler( 3 );
System.out.println();
printJuggler( 9 );
}
}
|
Python3
import math
def printJuggler(n) :
a = n
print (a,end = " " )
while (a ! = 1 ) :
b = 0
if (a % 2 = = 0 ) :
b = ( int )(math.floor(math.sqrt(a)))
else :
b = ( int ) (math.floor(math.sqrt(a) * math.sqrt(a) *
math.sqrt(a)))
print (b,end = " " )
a = b
printJuggler( 3 )
print ()
printJuggler( 9 )
|
C#
using System;
class GFG {
static void printJuggler( int n)
{
int a = n;
Console.Write(a+ " " );
while (a != 1)
{
int b = 0;
if (a%2 == 0)
b = ( int )Math.Floor(Math.Sqrt(a));
else
b =( int ) Math.Floor(Math.Sqrt(a) *
Math.Sqrt(a) * Math.Sqrt(a));
Console.Write( b+ " " );
a = b;
}
}
public static void Main () {
printJuggler(3);
Console.WriteLine();
printJuggler(9);
}
}
|
PHP
<?php
function printJuggler( $n )
{
$a = $n ;
echo ( $a . " " );
while ( $a != 1)
{
$b = 0;
if ( $a % 2 == 0)
$b = floor (sqrt( $a ));
else
$b = floor (sqrt( $a ) * sqrt( $a ) *
sqrt( $a ));
echo ( $b . " " );
$a = $b ;
}
}
printJuggler(3);
echo ( "\n" );
printJuggler(9);
?>
|
Javascript
<script>
function printJuggler(n)
{
let a = n;
document.write(a+ " " );
while (a != 1)
{
let b = 0;
if (a%2 == 0)
b = Math.floor(Math.sqrt(a));
else
b = Math.floor(Math.sqrt(a) *
Math.sqrt(a) * Math.sqrt(a));
document.write( b+ " " );
a = b;
}
}
printJuggler(3);
document.write( "<br/>" );
printJuggler(9);
</script>
|
Output:
3 5 11 36 6 2 1
9 27 140 11 36 6 2 1
Time complexity: O(nlogn) since using a single while loop and finding square root takes logarithmic time.
Space complexity: O(1) for constant variables
Important Points:
- The terms in Juggler Sequence first increase to a peak value and then start decreasing.
- The last term in Juggler Sequence is always 1.
Reference:
https://en.wikipedia.org/wiki/Juggler_sequence
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...