Skip to content

Instantly share code, notes, and snippets.

@benfletcher
Last active March 8, 2019 15:47
Show Gist options
  • Save benfletcher/3e12ad8d0c1700ecb5482554f41d4064 to your computer and use it in GitHub Desktop.
Save benfletcher/3e12ad8d0c1700ecb5482554f41d4064 to your computer and use it in GitHub Desktop.
Collatz Collusion

The Collatz conjecture:wiki

Start with any positive integer n. Then each term is obtained from the previous term as follows:

  • if the previous term is even, the next term is one half the previous term ( n / 2)
  • if the previous term is odd, the next term is 3 times the previous term plus 1 ( 3n + 1 )

The conjecture is that no matter what value of n, the sequence will always reach 1.

Numbers form a tree as they work their way down to 1, as seen in the image below. For example, starting with the number 5 you next get 16 ( 3x5 + 1 ) and then 8 ( 16 / 2 ). Starting with the number 64, you get 32 ( 64 / 2 ) and then 32. So the numbers 5 and 64 converge at 16.

Coding problem:

Given an array of 2 numbers, return the number where the branches converge.

Examples

Sample input: [24, 26] Answer: 10

Bonus: accept an arbitrary number of two or more numbers.

Sample input [28, 29, 30] Answer: 40

collatz tree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment