Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Juggler sequences

+6
−0

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
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (1 comment)

9 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+2
−0

JavaScript, 37 32 bytes

Outputs a comma delimited string.

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

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+6
−0

Jelly, 7 bytes

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

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+6
−0

BQN, 17 bytesSBCS

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

Run online!

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+5
−0

Husk, 11 10 bytes

U¡λ⌊^+.%2¹

Try it online!

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
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+5
−0

Scala, 78 67 64 bytes

Saved 3 bytes thanks to Razetime

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

Try it in Scastie!

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
History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
+3
−0

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
}
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−0

APL (Dyalog Unicode), 15 bytesSBCS

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

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Japt, 17 bytes

Needs some more work.

É?[U]cßÂUp½+Uu:1ì

Try it

If there's a proof that the length of the sequence for any given n never exceeds n**3 then ...

Japt, 14 bytes

NcU³Æ=Âp½+UuÃâ

Try it

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Python 3, 64 bytes

def f(n):
 z=[n]
 while n>1:n=int(n**((n+.5)%2));z+=n,
 return z

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)

Sign up to answer this question »