📜  杂耍序列

📅  最后修改于: 2021-04-28 17:26:51             🧑  作者: Mango

Juggler序列是一系列整数,其中第一项以正整数a开头,其余项是使用下面的递归关系从上一个紧邻项生成的:
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}
从数字3开始的杂耍序列:
5、11、36、6、2、1
从数字9开始的变戏法者序列:
9,27,140,11,36,6,2,1
给定数字n,我们必须打印该数字的Juggler序列作为序列的第一项。
例子:

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++
// C++ implementation of Juggler Sequence
#include 
using namespace std;
 
// This function prints the juggler Sequence
void printJuggler(int n)
{
    int a = n;
 
    // print the first term
    cout << a << " ";
 
    // calculate terms until
    // last term is not 1
    while (a != 1)
    {
        int b = 0;
 
        // Check if previous term
        // is even or odd
        if (a % 2 == 0)
 
            // calculate next term
            b = floor(sqrt(a));
 
        else
 
            // for odd previous term
            // calculate next term
            b = floor(sqrt(a) *
                      sqrt(a) * sqrt(a));
 
        cout << b << " ";
        a = b;
    }
}
 
// Driver Code
int main()
{
    printJuggler(3);
    cout <<"\n";
    printJuggler(9);
    return 0;
}
 
// This code is contributed by shubhamsingh10


C
// C implementation of Juggler Sequence
#include
#include
 
// This function prints the juggler Sequence
void printJuggler(int n)
{
    int a = n;
 
    // print the first term
    printf("%d ", a);
 
    // calculate terms until last term is not 1
    while (a != 1)
    {
        int b = 0;
 
        // Check if previous term is even or odd
        if (a%2 == 0)
 
            // calculate next term
            b  = floor(sqrt(a));
 
        else
 
            // for odd previous term calculate
            // next term
            b = floor(sqrt(a)*sqrt(a)*sqrt(a));
 
        printf("%d ", b);
        a = b;
    }
}
 
//driver program to test above function
int main()
{
    printJuggler(3);
    printf("\n");
    printJuggler(9);
    return 0;
}


Java
// Java implementation of Juggler Sequence
import java.io.*;
import java.math.*;
 
class GFG {
      
    // This function prints the juggler Sequence
    static void printJuggler(int n)
    {
        int a = n;
   
       // print the first term
       System.out.print(a+" ");
   
      // calculate terms until last term is not 1
       while (a != 1)
       {
          int b = 0;
    
          // Check if previous term is even or odd
          if (a%2 == 0)
    
             // calculate next term
                b  = (int)Math.floor(Math.sqrt(a));
   
          else
   
            // for odd previous term calculate
            // next term
                b =(int) Math.floor(Math.sqrt(a) *
                               Math.sqrt(a) * Math.sqrt(a));
   
          System.out.print( b+" ");
          a = b;
        }
    }
 
// Driver program to test above function
public static void main (String[] args) {
    printJuggler(3);
    System.out.println();
    printJuggler(9);
    }
}
  
//This code is contributed by Nikita Tiwari.


Python
import math
 
#This function prints the juggler Sequence
def printJuggler(n) :
    a = n
     
    # print the first term
    print a,
     
    # calculate terms until last term is not 1
    while (a != 1) :
        b = 0
         
        # Check if previous term is even or odd
        if (a%2 == 0) :
             
            # calculate next term
            b  = (int)(math.floor(math.sqrt(a)))
  
        else :
            # for odd previous term calculate
            # next term
            b = (int) (math.floor(math.sqrt(a)*math.sqrt(a)*
                                         math.sqrt(a)))
  
        print b,
        a = b
 
printJuggler(3)
print
printJuggler(9)
 
# This code is contributed by Nikita Tiwari.


C#
// C# implementation of Juggler Sequence
using System;
 
class GFG {
     
    // This function prints the juggler Sequence
    static void printJuggler(int n)
    {
        int a = n;
 
    // print the first term
    Console.Write(a+" ");
 
    // calculate terms until last term is not 1
    while (a != 1)
    {
        int b = 0;
     
        // Check if previous term is even or odd
        if (a%2 == 0)
     
            // calculate next term
                b = (int)Math.Floor(Math.Sqrt(a));
 
        else
 
            // for odd previous term calculate
            // next term
                b =(int) Math.Floor(Math.Sqrt(a) *
                     Math.Sqrt(a) * Math.Sqrt(a));
 
        Console.Write( b+" ");
        a = b;
        }
    }
 
// Driver Code
public static void Main () {
    printJuggler(3);
    Console.WriteLine();
    printJuggler(9);
    }
}
 
// This code is contributed by Nitin Mittal


PHP


Javascript


输出:

3 5 11 36 6 2 1 
9 27 140 11 36 6 2 1

重要事项:

  • 杂耍序列中的项首先增加到一个峰值,然后开始减小。
  • 杂耍者序列中的最后一项始终为1。