Skipping terms in the Collatz sequence
Doesn't this optimization in the collatz function result in missing terms from the sequence that change the visualization?
https://github.com/CodingTrain/website/blob/71e9709f77962671d2db2803f8611cf4fc87a98b/challenges/coding-in-the-cabana/002_collatz_conjecture/P5/sketch.js#L45
By returning (3 * n + 1) / 2 instead of just 3 * n + 1 for odd numbers, you end up skipping even numbers. For example when n = 3, the sequence should be [3, 10, 5, 16, 8, 4, 2, 1], but this function returns [3, 5, 16, 8, 4, 2, 1]. The 10 is missing from the sequence.
@darthmall Yes it skips some terms but this makes the visualisation kinda neat.
actually "(3 * n + 1) / 2" should return [3, 5, 8, 4, 2, 1] because it skipps one number after every odd one
Hi Darthmall!
The algorithm can definitely be improved and changed for actual use, for the website however we tend to stick to the examples from the videos! Your points is valid for sure though!
P.S. you should join our community discord for questions and remarks you might have in the future!