shorter collatz sequence? #120
Replies: 1 comment
-
Thank you for this. Inspired by it, and with some help from a code colfer, I managed to come up with
Anyway, there are basically two parts; recursion vs iteration and direct formula vs branching. We can compare the performance of all four as follows:
So, the current solution (iterative with branching) is fastest. Note that these recursive solutions take advantage of TCO, while yours doesn't, as it needs to wait for
And if we're after golfing, an adaption of this is the shortest I've seen:
|
Beta Was this translation helpful? Give feedback.
-
I came up with a shorter way to generate the collatz sequence. I'm not sure if it can be as easily used, but if anyone is willing to take a look here it is:
l←{⊃⍵=1:⍵⋄⍵,∇{2|⍵:1+3×⍵⋄⍵÷2}⊃⍵}
Beta Was this translation helpful? Give feedback.
All reactions