Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2954ed2

Browse files
Add Juggler Sequence (TheAlgorithms#2845)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parente488b7b commit2954ed2

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp