Skip to content

Commit 2954ed2

Browse files
Add Juggler Sequence (TheAlgorithms#2845)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parent e488b7b commit 2954ed2

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.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+
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+
64+
}

0 commit comments

Comments
 (0)