|
| 1 | +packagecom.thealgorithms.maths; |
| 2 | + |
| 3 | +/* |
| 4 | + * Java program for printing juggler sequence |
| 5 | + * Wikipedia: https://en.wikipedia.org/wiki/Juggler_sequence |
| 6 | + * |
| 7 | + * Author: Akshay Dubey (https://github.com/itsAkshayDubey) |
| 8 | + * |
| 9 | + * */ |
| 10 | + |
| 11 | +publicclassJugglerSequence { |
| 12 | + |
| 13 | + |
| 14 | +/** |
| 15 | + * This method prints juggler sequence starting with the number in the parameter |
| 16 | + * |
| 17 | + * @param inputNumber Number from which juggler sequence is to be started |
| 18 | + * */ |
| 19 | +staticvoidjugglerSequence(intinputNumber) { |
| 20 | +//Copy method argument to a local variable |
| 21 | +intn =inputNumber; |
| 22 | + |
| 23 | +//Printing first number |
| 24 | +System.out.print(n+","); |
| 25 | + |
| 26 | +//Looping till n reaches 1 |
| 27 | +while(n !=1) { |
| 28 | +inttemp=0; |
| 29 | + |
| 30 | +//if previous term is even then |
| 31 | +// next term in the sequence is square root of previous term |
| 32 | +//if previous term is odd then |
| 33 | +// next term is floor value of 3 time the square root of previous term |
| 34 | + |
| 35 | +//Check if previous term is even or odd |
| 36 | +if(n%2 ==0) { |
| 37 | +temp = (int)Math.floor(Math.sqrt(n)); |
| 38 | +} |
| 39 | +else { |
| 40 | +temp = (int)Math.floor(Math.sqrt(n)*Math.sqrt(n)*Math.sqrt(n)); |
| 41 | +} |
| 42 | + |
| 43 | +//Printing next term |
| 44 | +if(temp !=1) { |
| 45 | +System.out.print(temp+","); |
| 46 | +} |
| 47 | +else{ |
| 48 | +System.out.print(temp); |
| 49 | +} |
| 50 | + |
| 51 | +n =temp; |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +//Driver code |
| 58 | +publicstaticvoidmain(String[]args) { |
| 59 | +jugglerSequence(3); |
| 60 | + |
| 61 | +//Output: 3,5,11,36,6,2,1 |
| 62 | +} |
| 63 | + |
| 64 | +} |