Zsonglőr sorozat
#practiceLinkDiv { display: none !important; } A zsonglőrsorozat olyan egész számsorozat, amelyben az első tag pozitív egész számmal kezdődik a és a fennmaradó kifejezések a közvetlen előző kifejezésből jönnek létre az alábbi ismétlődési reláció használatával:
egy
Zsonglőr sorozat a 3-as számmal kezdődően:
3 5 11 36 6 2 1
Zsonglőr sorozat a 9-es számmal kezdődően:
9 27 140 11 36 6 2 1
Adott egy számot n ehhez a számhoz ki kell nyomtatnunk a Zsonglőr sorozatot a sorozat első tagjaként.
Példák:
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 1Recommended Practice Zsonglőr sorozat Próbáld ki! C++
// C++ implementation of Juggler Sequence #include using namespace std ; // This function prints the juggler Sequence void printJuggler ( long long n ) { long long a = n ; // print the first term cout < < a < < ' ' ; // calculate terms until // last term is not 1 while ( a != 1 ) { long long 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 ( 37 ); 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.
Python3 import math #This function prints the juggler Sequence def printJuggler ( n ) : a = n # print the first term print ( a end = ' ' ) # 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 end = ' ' ) 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 // PHP implementation of // Juggler Sequence // function prints the // juggler Sequence function printJuggler ( $n ) { $a = $n ; // print the first term echo ( $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 = floor ( sqrt ( $a )); else // for odd previous term // calculate next term $b = floor ( sqrt ( $a ) * sqrt ( $a ) * sqrt ( $a )); echo ( $b . ' ' ); $a = $b ; } } // Driver Code printJuggler ( 3 ); echo ( ' n ' ); printJuggler ( 9 ); // This code is contributed by Ajit. ?>
JavaScript < script > // Javascript implementation of Juggler Sequence // This function prints the juggler Sequence function printJuggler ( n ) { let a = n ; // print the first term document . write ( a + ' ' ); // calculate terms until last term is not 1 while ( a != 1 ) { let b = 0 ; // Check if previous term is even or odd if ( a % 2 == 0 ) // calculate next term b = Math . floor ( Math . sqrt ( a )); else // for odd previous term calculate // next term b = Math . floor ( Math . sqrt ( a ) * Math . sqrt ( a ) * Math . sqrt ( a )); document . write ( b + ' ' ); a = b ; } } // Driver code to test above methods printJuggler ( 3 ); document . write ( '
' ); printJuggler ( 9 ); // This code is contributed by avijitmondal1998. < /script>
Kimenet:
3 5 11 36 6 2 1 9 27 140 11 36 6 2 1
Időbeli összetettség : O(nlogn), mivel egyetlen while ciklus használata és a négyzetgyök keresése logaritmikus időt vesz igénybe.
A tér összetettsége : O(1) állandó változók esetén
Fontos pontok:
- A Juggler Sequence kifejezései először csúcsértékre nőnek, majd csökkenni kezdenek.
- A zsonglőrsorozat utolsó tagja mindig 1.
Referencia:
https://en.wikipedia.org/wiki/Juggler_sequence