Skip to main content

Juggler Sequence

The Juggler Sequence is a mathematical sequence that begins with a given positive integer and generates subsequent terms based on whether the current term is even or odd. This sequence has intriguing properties and can produce a wide variety of patterns. The sequence starts with a given positive integer 'n', and each term 'a' in the sequence is calculated using the following rules:

  • If 'a' is even, the next term is the floor value of the square root of 'a'.
  • If 'a' is odd, the next term is the floor value of 'a' raised to the power of 1.5 (3/2).

Problem Statement

Given a positive integer 'n', the goal is to generate and print the Juggler Sequence starting from 'n' and continuing until the sequence reaches 1.

Example

Let's take the initial value 'n = 10' to demonstrate the Juggler Sequence generation:

  1. Start with 10.
  2. Since 10 is even, the next term is the floor value of √10 ≈ 3.
  3. Since 3 is odd, the next term is the floor value of 3^1.5 ≈ 5.196 ≈ 5.
  4. Continuing this process, we get the sequence: 10 3 5 11 36 6 2 1.

Idea to Solve

The problem can be solved using an iterative approach. Given the current term 'value', the next term can be calculated based on whether 'value' is even or odd. The calculated term is then updated as the new 'value', and the process continues until 'value' becomes 1.

Pseudocode

function jugglerSequence(n):
    value = n
    print "Juggler Sequence start with", n, "is"
    while value > 1:
        print value,
        if value is even:
            value = floor(sqrt(value))
        else:
            value = floor(value^1.5)
    print 1

Algorithm Explanation

  1. Start with the given value 'n'.
  2. Initialize 'value' with 'n'.
  3. Print the initial message indicating the start of the sequence.
  4. Enter a loop that continues until 'value' becomes 1: a. Print the current 'value'. b. Check if 'value' is even:
    • If it is, update 'value' as the floor value of √value. c. If 'value' is odd, update 'value' as the floor value of value^1.5.
  5. After the loop ends, print 1 to complete the sequence.

Code Solution

// C Program for
// Juggler Sequence
#include <stdio.h>
#include <math.h>

void jugglerSequence(int n)
{
    if (n <= 1)
    {
        return;
    }
    printf("\n Juggler Sequence start with %d is \n", n);
    int value = n;
    while (value > 1)
    {
        printf(" %d", value);
        if ((value % 2) == 0)
        {
            // When value is even
            /*
                formula
                ----------
                v^(1/2) = v ^ (0.5)
            */
            value = floor(pow(value, 0.5));
        }
        else
        {
            // When value is odd
            /*
                formula
                ----------
                v^(3.0/2.0) = v ^ (1.5)
            */
            value = floor(pow(value, 1.5));
        }
    }
    // Last element
    printf(" 1\n");
}
int main()
{
    // Test
    jugglerSequence(10);
    jugglerSequence(5);
    return 0;
}

Output

 Juggler Sequence start with 10 is
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is
 5 11 36 6 2 1
// Java program for
// Juggler Sequence
public class Sequence
{
    public void jugglerSequence(int n)
    {
        if (n <= 1)
        {
            return;
        }
        System.out.print("\n Juggler Sequence start with " + n + " is \n");
        int value = n;
        while (value > 1)
        {
            System.out.print(" " + value);
            if ((value % 2) == 0)
            {
                // When value is even
                /*
                    formula
                    ----------
                    v^(1/2) = v ^ (0.5)
                */
                value = (int)Math.floor(Math.pow(value, 0.5));
            }
            else
            {
                // When value is odd
                /*
                    formula
                    ----------
                    v^(3.0/2.0) = v ^ (1.5)
                */
                value = (int)Math.floor(Math.pow(value, 1.5));
            }
        }
        // Last element
        System.out.print(" 1\n");
    }
    public static void main(String[] args)
    {
        Sequence task = new Sequence();
        // Test
        task.jugglerSequence(10);
        task.jugglerSequence(5);
    }
}

Output

 Juggler Sequence start with 10 is
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is
 5 11 36 6 2 1
// Include header file
#include <iostream>

#include <math.h>

using namespace std;
// C++ program for
// Juggler Sequence
class Sequence
{
    public: void jugglerSequence(int n)
    {
        if (n <= 1)
        {
            return;
        }
        cout << "\n Juggler Sequence start with " << n << " is \n";
        int value = n;
        while (value > 1)
        {
            cout << " " << value;
            if ((value % 2) == 0)
            {
                // When value is even
                /*
                    formula
                    ----------
                    v^(1/2) = v ^ (0.5)
                */
                value = (int) floor(pow(value, 0.5));
            }
            else
            {
                // When value is odd
                /*
                    formula
                    ----------
                    v^(3.0/2.0) = v ^ (1.5)
                */
                value = (int) floor(pow(value, 1.5));
            }
        }
        // Last element
        cout << " 1\n";
    }
};
int main()
{
    Sequence *task = new Sequence();
    // Test
    task->jugglerSequence(10);
    task->jugglerSequence(5);
    return 0;
}

Output

 Juggler Sequence start with 10 is
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is
 5 11 36 6 2 1
// Include namespace system
using System;
// Csharp program for
// Juggler Sequence
public class Sequence
{
    public void jugglerSequence(int n)
    {
        if (n <= 1)
        {
            return;
        }
        Console.Write("\n Juggler Sequence start with " + n + " is \n");
        int value = n;
        while (value > 1)
        {
            Console.Write(" " + value);
            if ((value % 2) == 0)
            {
                // When value is even
                /*
                    formula
                    ----------
                    v^(1/2) = v ^ (0.5)
                */
                value = (int) Math.Floor(Math.Pow(value, 0.5));
            }
            else
            {
                // When value is odd
                /*
                    formula
                    ----------
                    v^(3.0/2.0) = v ^ (1.5)
                */
                value = (int) Math.Floor(Math.Pow(value, 1.5));
            }
        }
        // Last element
        Console.Write(" 1\n");
    }
    public static void Main(String[] args)
    {
        Sequence task = new Sequence();
        // Test
        task.jugglerSequence(10);
        task.jugglerSequence(5);
    }
}

Output

 Juggler Sequence start with 10 is
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is
 5 11 36 6 2 1
package main
import "math"
import "fmt"
// Go program for
// Juggler Sequence

func jugglerSequence(n int) {
    if n <= 1 {
        return
    }
    fmt.Print("\n Juggler Sequence start with ", n, " is \n")
    var value int = n
    for (value > 1) {
        fmt.Print(" ", value)
        if (value % 2) == 0 {
            // When value is even
            /*
                formula
                ----------
                v^(1/2) = v ^ (0.5)
            */
            value = int(math.Floor(math.Pow(float64(value), 0.5)))
        } else {
            // When value is odd
            /*
                formula
                ----------
                v^(3.0/2.0) = v ^ (1.5)
            */
            value = int(math.Floor(math.Pow(float64(value), 1.5)))
        }
    }
    // Last element
    fmt.Print(" 1\n")
}
func main() {
    
    // Test
    jugglerSequence(10)
    jugglerSequence(5)
}

Output

 Juggler Sequence start with 10 is
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is
 5 11 36 6 2 1
<?php
// Php program for
// Juggler Sequence
class Sequence
{
    public  function jugglerSequence($n)
    {
        if ($n <= 1)
        {
            return;
        }
        echo("\n Juggler Sequence start with ".$n.
            " is \n");
        $value = $n;
        while ($value > 1)
        {
            echo(" ".$value);
            if (($value % 2) == 0)
            {
                // When value is even
                /*
                    formula
                    ----------
                    v^(1/2) = v ^ (0.5)
                */
                $value = (int) floor(pow($value, 0.5));
            }
            else
            {
                // When value is odd
                /*
                    formula
                    ----------
                    v^(3.0/2.0) = v ^ (1.5)
                */
                $value = (int) floor(pow($value, 1.5));
            }
        }
        // Last element
        echo(" 1\n");
    }
}

function main()
{
    $task = new Sequence();
    // Test
    $task->jugglerSequence(10);
    $task->jugglerSequence(5);
}
main();

Output

 Juggler Sequence start with 10 is
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is
 5 11 36 6 2 1
// Node JS program for
// Juggler Sequence
class Sequence
{
    jugglerSequence(n)
    {
        if (n <= 1)
        {
            return;
        }
        process.stdout.write("\n Juggler Sequence start with " + 
                             n + " is \n");
        var value = n;
        while (value > 1)
        {
            process.stdout.write(" " + value);
            if ((value % 2) == 0)
            {
                // When value is even
                /*
                    formula
                    ----------
                    v^(1/2) = v ^ (0.5)
                */
                value =  Math.floor(Math.pow(value, 0.5));
            }
            else
            {
                // When value is odd
                /*
                    formula
                    ----------
                    v^(3.0/2.0) = v ^ (1.5)
                */
                value = Math.floor(Math.pow(value, 1.5));
            }
        }
        // Last element
        process.stdout.write(" 1\n");
    }
}

function main()
{
    var task = new Sequence();
    // Test
    task.jugglerSequence(10);
    task.jugglerSequence(5);
}
main();

Output

 Juggler Sequence start with 10 is
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is
 5 11 36 6 2 1
import math
#  Python 3 program for
#  Juggler Sequence
class Sequence :
    def jugglerSequence(self, n) :
        if (n <= 1) :
            return
        
        print("\n Juggler Sequence start with ", n ," is ")
        value = n
        while (value > 1) :
            print(" ", value, end = "")
            if ((value % 2) == 0) :
                #  When value is even
                #    formula
                #    ----------
                #    v^(1/2) = v ^ (0.5)
                value = math.floor(value ** 0.5)
            else :
                #  When value is odd
                #    formula
                #    ----------
                #    v^(3.0/2.0) = v ^ (1.5)
                value =  math.floor(value ** 1.5)
            
        
        #  Last element
        print(" 1")
    

def main() :
    task = Sequence()
    #  Test
    task.jugglerSequence(10)
    task.jugglerSequence(5)

if __name__ == "__main__": main()

Output

 Juggler Sequence start with  10  is
  10  3  5  11  36  6  2 1

 Juggler Sequence start with  5  is
  5  11  36  6  2 1
#  Ruby program for
#  Juggler Sequence
class Sequence 
    def jugglerSequence(n) 
        if (n <= 1) 
            return
        end

        print("\n Juggler Sequence start with ", n ," is \n")
        value = n
        while (value > 1) 
            print(" ", value)
            if ((value % 2) == 0) 
                #  When value is even
                #    formula
                #    ----------
                #    v^(1/2) = v ^ (0.5)
                value = (value ** 0.5).floor()
            else
 
                #  When value is odd
                #    formula
                #    ----------
                #    v^(3.0/2.0) = v ^ (1.5)
                value = (value ** 1.5).floor()
            end

        end

        #  Last element
        print(" 1\n")
    end

end

def main() 
    task = Sequence.new()
    #  Test
    task.jugglerSequence(10)
    task.jugglerSequence(5)
end

main()

Output

 Juggler Sequence start with 10 is 
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is 
 5 11 36 6 2 1
// Scala program for
// Juggler Sequence
class Sequence()
{
    def jugglerSequence(n: Int): Unit = {
        if (n <= 1)
        {
            return;
        }
        print("\n Juggler Sequence start with " + n + " is \n");
        var value: Int = n;
        while (value > 1)
        {
            print(" " + value);
            if ((value % 2) == 0)
            {
                // When value is even
                /*
                    formula
                    ----------
                    v^(1/2) = v ^ (0.5)
                */
                value = Math.floor(Math.pow(value, 0.5)).toInt;
            }
            else
            {
                // When value is odd
                /*
                    formula
                    ----------
                    v^(3.0/2.0) = v ^ (1.5)
                */
                value = Math.floor(Math.pow(value, 1.5)).toInt;
            }
        }
        // Last element
        print(" 1\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Sequence = new Sequence();
        // Test
        task.jugglerSequence(10);
        task.jugglerSequence(5);
    }
}

Output

 Juggler Sequence start with 10 is
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is
 5 11 36 6 2 1
import Foundation;
// Swift 4 program for
// Juggler Sequence
class Sequence
{
    func jugglerSequence(_ n: Int)
    {
        if (n <= 1)
        {
            return;
        }
        print("\n Juggler Sequence start with ", n ," is ");
        var value: Int = n;
        while (value > 1)
        {
            print(" ", value, terminator: "");
            if ((value % 2) == 0)
            {
                // When value is even
                /*
                    formula
                    ----------
                    v^(1/2) = v ^ (0.5)
                */
                value = Int(floor(pow(Double(value), 0.5)));
            }
            else
            {
                // When value is odd
                /*
                    formula
                    ----------
                    v^(3.0/2.0) = v ^ (1.5)
                */
                value = Int(floor(pow(Double(value), 1.5)));
            }
        }
        // Last element
        print(" 1");
    }
}
func main()
{
    let task: Sequence = Sequence();
    // Test
    task.jugglerSequence(10);
    task.jugglerSequence(5);
}
main();

Output

 Juggler Sequence start with  10  is
  10  3  5  11  36  6  2 1

 Juggler Sequence start with  5  is
  5  11  36  6  2 1
// Kotlin program for
// Juggler Sequence
class Sequence
{
    fun jugglerSequence(n: Int): Unit
    {
        if (n <= 1)
        {
            return;
        }
        print("\n Juggler Sequence start with " + n + " is \n");
        var value: Int = n;
        while (value > 1)
        {
            print(" " + value);
            if ((value % 2) == 0)
            {
                // When value is even
                /*
                    formula
                    ----------
                    v^(1/2) = v ^ (0.5)
                */
                value = Math.floor(Math.pow(value.toDouble(), 0.5)).toInt();
            }
            else
            {
                // When value is odd
                /*
                    formula
                    ----------
                    v^(3.0/2.0) = v ^ (1.5)
                */
                value = Math.floor(Math.pow(value.toDouble(), 1.5)).toInt();
            }
        }
        // Last element
        print(" 1\n");
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Sequence = Sequence();
    // Test
    task.jugglerSequence(10);
    task.jugglerSequence(5);
}

Output

 Juggler Sequence start with 10 is
 10 3 5 11 36 6 2 1

 Juggler Sequence start with 5 is
 5 11 36 6 2 1

Time Complexity

The time complexity of the code is determined by the number of iterations in the while loop. The loop runs until the 'value' becomes 1. In the worst case, the number of iterations required to reach 1 can be considered logarithmic with base 2, which is O(log n), where 'n' is the initial value. The computations inside the loop (calculating square root or raising to the power) are relatively inexpensive compared to the loop itself. Therefore, the overall time complexity is dominated by the loop iteration count.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment