|
1 | 1 | package com.thealgorithms.maths;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
3 | 6 | /*
|
4 | 7 | * Java program for printing juggler sequence
|
5 | 8 | * Wikipedia: https://en.wikipedia.org/wiki/Juggler_sequence
|
|
9 | 12 | * */
|
10 | 13 |
|
11 | 14 | public class JugglerSequence {
|
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 |
| - static void jugglerSequence(int inputNumber) { |
20 |
| - //Copy method argument to a local variable |
21 |
| - int n = inputNumber; |
22 |
| - |
23 |
| - //Printing first number |
24 |
| - System.out.print(n+","); |
25 |
| - |
26 |
| - //Looping till n reaches 1 |
27 |
| - while(n != 1) { |
28 |
| - int temp=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 |
| - public static void main(String[] args) { |
59 |
| - jugglerSequence(3); |
60 |
| - |
61 |
| - //Output: 3,5,11,36,6,2,1 |
62 |
| - } |
63 |
| - |
| 15 | + /** |
| 16 | + * This method prints juggler sequence starting with the number in the parameter |
| 17 | + * |
| 18 | + * @param inputNumber Number from which juggler sequence is to be started |
| 19 | + */ |
| 20 | + public static void jugglerSequence(int inputNumber) { |
| 21 | + // Copy method argument to a local variable |
| 22 | + int n = inputNumber; |
| 23 | + List<String> seq = new ArrayList<>(); |
| 24 | + seq.add(n + ""); |
| 25 | + // Looping till n reaches 1 |
| 26 | + while (n != 1) { |
| 27 | + int temp; |
| 28 | + // if previous term is even then |
| 29 | + // next term in the sequence is square root of previous term |
| 30 | + // if previous term is odd then |
| 31 | + // next term is floor value of 3 time the square root of previous term |
| 32 | + |
| 33 | + // Check if previous term is even or odd |
| 34 | + if (n % 2 == 0) { |
| 35 | + temp = (int) Math.floor(Math.sqrt(n)); |
| 36 | + } else { |
| 37 | + temp = (int) Math.floor(Math.sqrt(n) * Math.sqrt(n) * Math.sqrt(n)); |
| 38 | + } |
| 39 | + n = temp; |
| 40 | + seq.add(n + ""); |
| 41 | + } |
| 42 | + String res = String.join(",", seq); |
| 43 | + System.out.println(res); |
| 44 | + } |
| 45 | + |
| 46 | + // Driver code |
| 47 | + public static void main(String[] args) { |
| 48 | + jugglerSequence(3); |
| 49 | + // Output: 3,5,11,36,6,2,1 |
| 50 | + } |
64 | 51 | }
|
0 commit comments