Fix formatting in Juggler Sequence (#2846)

This commit is contained in:
Yang Libin 2021-11-27 18:55:32 +08:00 committed by GitHub
parent 2954ed2ab1
commit b870de4db4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 50 deletions

View File

@ -195,6 +195,7 @@
* [AutomorphicNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java)
* [Average](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Average.java)
* [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinaryPow.java)
* [BinomialCoefficient](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinomialCoefficient.java)
* [Ceil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Ceil.java)
* [CircularConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/CircularConvolutionFFT.java)
* [Combinations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Combinations.java)
@ -220,6 +221,7 @@
* [GCDRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/GCDRecursion.java)
* [GenericRoot](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/GenericRoot.java)
* [HarshadNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/HarshadNumber.java)
* [JugglerSequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/JugglerSequence.java)
* [KeithNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/KeithNumber.java)
* [KrishnamurthyNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java)
* [LeonardoNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LeonardoNumber.java)
@ -289,6 +291,7 @@
* [FirstFit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/FirstFit.java)
* [FloydTriangle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/FloydTriangle.java)
* [GuassLegendre](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/GuassLegendre.java)
* [HappyNumbersSeq](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/HappyNumbersSeq.java)
* [Huffman](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/Huffman.java)
* [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)
* [InsertDeleteInArray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java)
@ -334,6 +337,7 @@
* [IterativeTernarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/IterativeTernarySearch.java)
* [JumpSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/JumpSearch.java)
* [LinearSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/LinearSearch.java)
* [LinearSearchThread](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/LinearSearchThread.java)
* [LowerBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/LowerBound.java)
* [MonteCarloTreeSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/MonteCarloTreeSearch.java)
* [PerfectBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/PerfectBinarySearch.java)

View File

@ -1,5 +1,8 @@
package com.thealgorithms.maths;
import java.util.ArrayList;
import java.util.List;
/*
* Java program for printing juggler sequence
* Wikipedia: https://en.wikipedia.org/wiki/Juggler_sequence
@ -9,56 +12,40 @@ package com.thealgorithms.maths;
* */
public class JugglerSequence {
/**
* This method prints juggler sequence starting with the number in the parameter
*
* @param inputNumber Number from which juggler sequence is to be started
*/
public static void jugglerSequence(int inputNumber) {
// Copy method argument to a local variable
int n = inputNumber;
List<String> seq = new ArrayList<>();
seq.add(n + "");
// Looping till n reaches 1
while (n != 1) {
int temp;
// if previous term is even then
// next term in the sequence is square root of previous term
// if previous term is odd then
// next term is floor value of 3 time the square root of previous term
// Check if previous term is even or odd
if (n % 2 == 0) {
temp = (int) Math.floor(Math.sqrt(n));
} else {
temp = (int) Math.floor(Math.sqrt(n) * Math.sqrt(n) * Math.sqrt(n));
}
n = temp;
seq.add(n + "");
}
String res = String.join(",", seq);
System.out.println(res);
}
/**
* This method prints juggler sequence starting with the number in the parameter
*
* @param inputNumber Number from which juggler sequence is to be started
* */
static void jugglerSequence(int inputNumber) {
//Copy method argument to a local variable
int n = inputNumber;
//Printing first number
System.out.print(n+",");
//Looping till n reaches 1
while(n != 1) {
int temp=0;
//if previous term is even then
// next term in the sequence is square root of previous term
//if previous term is odd then
// next term is floor value of 3 time the square root of previous term
//Check if previous term is even or odd
if(n%2 == 0) {
temp = (int) Math.floor(Math.sqrt(n));
}
else {
temp = (int) Math.floor(Math.sqrt(n)*Math.sqrt(n)*Math.sqrt(n));
}
//Printing next term
if(temp != 1) {
System.out.print(temp+",");
}
else{
System.out.print(temp);
}
n = temp;
}
}
//Driver code
public static void main(String[] args) {
jugglerSequence(3);
//Output: 3,5,11,36,6,2,1
}
// Driver code
public static void main(String[] args) {
jugglerSequence(3);
// Output: 3,5,11,36,6,2,1
}
}