Skip to content

Commit b870de4

Browse files
authored
Fix formatting in Juggler Sequence (TheAlgorithms#2846)
1 parent 2954ed2 commit b870de4

File tree

2 files changed

+43
-52
lines changed

2 files changed

+43
-52
lines changed

DIRECTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
* [AutomorphicNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java)
196196
* [Average](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Average.java)
197197
* [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinaryPow.java)
198+
* [BinomialCoefficient](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinomialCoefficient.java)
198199
* [Ceil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Ceil.java)
199200
* [CircularConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/CircularConvolutionFFT.java)
200201
* [Combinations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Combinations.java)
@@ -220,6 +221,7 @@
220221
* [GCDRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/GCDRecursion.java)
221222
* [GenericRoot](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/GenericRoot.java)
222223
* [HarshadNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/HarshadNumber.java)
224+
* [JugglerSequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/JugglerSequence.java)
223225
* [KeithNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/KeithNumber.java)
224226
* [KrishnamurthyNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java)
225227
* [LeonardoNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LeonardoNumber.java)
@@ -289,6 +291,7 @@
289291
* [FirstFit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/FirstFit.java)
290292
* [FloydTriangle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/FloydTriangle.java)
291293
* [GuassLegendre](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/GuassLegendre.java)
294+
* [HappyNumbersSeq](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/HappyNumbersSeq.java)
292295
* [Huffman](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/Huffman.java)
293296
* [Implementing auto completing features using trie](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/Implementing_auto_completing_features_using_trie.java)
294297
* [InsertDeleteInArray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java)
@@ -334,6 +337,7 @@
334337
* [IterativeTernarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/IterativeTernarySearch.java)
335338
* [JumpSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/JumpSearch.java)
336339
* [LinearSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/LinearSearch.java)
340+
* [LinearSearchThread](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/LinearSearchThread.java)
337341
* [LowerBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/LowerBound.java)
338342
* [MonteCarloTreeSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/MonteCarloTreeSearch.java)
339343
* [PerfectBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/PerfectBinarySearch.java)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.thealgorithms.maths;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
/*
47
* Java program for printing juggler sequence
58
* Wikipedia: https://en.wikipedia.org/wiki/Juggler_sequence
@@ -9,56 +12,40 @@
912
* */
1013

1114
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+
}
6451
}

0 commit comments

Comments
 (0)