website-archive icon indicating copy to clipboard operation
website-archive copied to clipboard

Skipping terms in the Collatz sequence

Open darthmall opened this issue 5 years ago • 3 comments

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 avatar Oct 22 '21 14:10 darthmall

@darthmall Yes it skips some terms but this makes the visualisation kinda neat.

yathy2007 avatar Jan 08 '22 04:01 yathy2007

actually "(3 * n + 1) / 2" should return [3, 5, 8, 4, 2, 1] because it skipps one number after every odd one

Miir420 avatar Apr 01 '22 14:04 Miir420

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!

KobeLiesenborgs avatar May 16 '22 19:05 KobeLiesenborgs