# Juggler sequences

A Juggler sequence is a sequence that begins with a positive integer $a_0$ and each subsequent term is calculated as:

$$a_{k+1} = \begin{cases}
\left \lfloor a_k ^ \frac 1 2 \right \rfloor, & \text{if } a_k \text{ is even}\\

\left \lfloor a_k ^ \frac 3 2 \right \rfloor, & \text{if } a_k \text{ is odd}
\end{cases}$$

Eventually, once $a_k$ equals $1$, the sequence ends, as all subsequent terms will be $1$. It has been conjectured, but not proven, that all Juggler sequences reach 1.

Given a positive integer $n \ge 2$, output the Juggler sequence beginning with $a_0 = n$ and ending in $1$. You may assume it will always terminate. You should only output a single $1$, and the sequence should be in calculated order ($a_0, a_1, a_2,$ etc.)

This is code-golf, so the shortest code in bytes wins

## Test cases

```
2: 2, 1
3: 3, 5, 11, 36, 6, 2, 1
4: 4, 2, 1
5: 5, 11, 36, 6, 2, 1
6: 6, 2, 1
7: 7, 18, 4, 2, 1
8: 8, 2, 1
9: 9, 27, 140, 11, 36, 6, 2, 1
10: 10, 3, 5, 11, 36, 6, 2, 1
```

[Jelly], 7 bytes Ḃ×½ḞµƬ …

2y ago

BQN, 17 bytesSBCS ``` {𝕩∾1 …

2y ago

Scala, 78 67 64 bytes Saved …

2y ago

[Husk], 11 10 bytes U¡λ …

2y ago

JavaScript (Node.js), 73 66 by …

2y ago

[APL (Dyalog Unicode)], 15 byt …

2y ago

Japt, 17 bytes Needs some m …

2y ago

JavaScript, 37 32 bytes Out …

2y ago

[Python 3], 64 bytes …

2y ago

## 9 answers

#
BQN, 17 bytes^{SBCS}

```
{𝕩∾1𝕊⍟≠⌊𝕩⋆2|𝕩+÷2}
```

BQN primitives alone can't express unbounded iteration, so this must be a recursive block function.

```
{𝕩∾1𝕊⍟≠⌊𝕩⋆2|𝕩+÷2} # Function 𝕊 taking argument 𝕩
÷2 # Reciprocal of 2 (one half)
𝕩+ # Plus 𝕩
2| # Modulo 2
𝕩⋆ # 𝕩 power
⌊ # Floor
𝕊 # Keep going
1 ⍟≠ # ...if not 1
𝕩∾ # Prepend 𝕩
```

`1𝕊⍟≠…`

passes 1 as the left argument `𝕨`

, which is harmless.

#### 0 comment threads

# Jelly, 7 bytes

```
*Ḃ×½ḞµƬ
```

```
*Ḃ×½ḞµƬ
*Ḃ input ^ (input % 2)
×½ multiply sqrt(input)
Ḟ floor
µƬ iterate until converged and collect results
```

#### 0 comment threads

#
Husk, ^{11} 10 bytes

```
U¡λ⌊^+.%2¹
```

This can probably be trivially ported to Jelly.

## Explanation

```
U¡o⌊Ṡ^o+.%2
¡ iterate over the input infinitely, creating a list
o with the following two functions:
Ṡ^ take the previous result to the power
%2 result modulo 2
+. + 1/2
⌊ floor it
U longest unique prefix
```

#### 0 comment threads

# Scala, ~~78 67~~ 64 bytes

Saved 3 bytes thanks to Razetime

```
Stream.iterate(_)(x=>math.pow(x,x%2+.5).toInt).takeWhile(_>1):+1
```

```
Stream.iterate(_) //Make an infinite list by repeatedly applying
(x=> //the following function to the input
math.pow(x, //Raise x to the power of
x%2 //x%2 (0 or 1)
+.5 //plus .5 (.5 or 1.5)
).toInt //Floor it
).takeWhile(_>1) //Take all elements until it hits 1
:+1 //Append a 1
```

#
APL (Dyalog Unicode), 15 bytes^{SBCS}

With many thanks to Marshall Lochbaum and dzaima for their help debugging and golfing this answer

```
{⍵∪⌊⍵*2|⍵+.5}⍣≡
```

```
{⍵∪⌊⍵*2|⍵+.5}⍣≡ ⍝ ⍵ is our input of either n or our intermediate results
{ }⍣≡ ⍝ run until we reach a fixed point (in this case, 1)
⍵+.5 ⍝ ⍵ plus one half
2| ⍝ mod 2 gives us 0.5 if our ⍵ was even, else 1.5
⍵* ⍝ ⍵ to the power of the above exponent
⌊ ⍝ floor
⍵∪ ⍝ union of the answer with the previous results
⍝ The set union will either remove an extra one,
⍝ or if we find a cycle that disproves the conjecture,
⍝ will remove the extra members of the cycle
```

#### 0 comment threads

## JavaScript (Node.js), ~~73~~ 66 bytes

This returns a Generator, which calculates the sequence on the fly when you use it!

```
function*f(a){while(a>1){yield a
a=~~(a%2?a**1.5:a**0.5)}
yield a}
```

Original, more aesthetically pleasing but slightly longer version:

```
function*f(a){while(1){yield a
if(a==1){return}
a=~~(a%2?a**1.5:a**0.5)}}
```

To use it, do something like this, which calculates the whole thing and makes it into an array.

```
[...f(5)] // returns [ 5, 11, 36, 6, 2, 1 ]
```

Explanation:

```
function* f(a) { // makes a generator function
while (a > 1) {
yield a // return this bit of the sequence
a = ~~( // truncates the decimals? apparently? and it's shorter than Math.floor()/Math.trunc()
a%2 ? a**1.5 : a**0.5
)
}
yield a // return the final 1
}
```

#### 0 comment threads

# JavaScript, ~~37~~ 32 bytes

Outputs a comma delimited string.

```
f=n=>n-1?n+[,f(n**(.5+n%2)|0)]:n
```

## 1 comment thread