22
0
The Collatz Sequence (also called the 3x + 1 problem) is where you start with any positive integer, for this example we will use 10, and apply this set of steps to it:
if n is even:
Divide it by 2
if n is odd:
Multiply it by 3 and add 1
repeat until n = 1
10 is even, so we divide by 2 to get 5. 5 is odd, so we multiply by 3 and add 1 to get 16. 16 is even, so cut it in half to get 8. Half of 8 is 4, half of 4 is 2, and half of 2 is 1. Since this took us 6 steps, we say that 10 has a stopping distance of 6.
A Super Collatz number is a number whose stopping distance is greater then the stopping distance of every number smaller than it. For example, 6 is a Super Collatz number since 6 has a stopping distance of 8, 5 has a stopping distance of 5, 4 has 2, 3 has 7, 2 has 1 and 1 has 0. (A006877 in the OEIS) You must take a number n as input, and output out all the Super Collatz numbers up to n.
Rules
Full Program or function is acceptable.
You can not precompute or hard-code the Super Collatz sequence.
You can take input in any reasonable format.
Output can be returned as a list from the function, or printed to STDOUT or a file. Whichever is most convenient.
Invalid inputs (non-numbers, decimals, negative numbers, etc.) result in undefined behavior.
Sample ungolfed python
def collatzDist(n):
if n == 1:
return 0
if n % 2 == 0:
return 1 + collatzDist(n / 2)
return 1 + collatzDist((n * 3) + 1)
n = input()
max = -1
superCollatz = []
for i in range(1, n + 1):
dist = collatzDist(i)
if dist > max:
superCollatz.append(i)
max = dist
print superCollatz
Sample IO:
#in #out
4 --> 1, 2, 3
50 --> 1, 2, 3, 6, 7, 9, 18, 25, 27
0 --> invalid
10000 --> 1, 2, 3, 6, 7, 9, 18, 25, 27, 54, 73, 97, 129, 171, 231, 313, 327, 649, 703, 871, 1161, 2223, 2463, 2919, 3711, 6171
Also here are the first 44 Super Collatz numbers:
1, 2, 3, 6, 7, 9, 18, 25, 27, 54, 73, 97, 129, 171, 231, 313, 327, 649, 703, 871, 1161, 2223, 2463, 2919, 3711, 6171, 10971, 13255, 17647, 23529, 26623, 34239, 35655, 52527, 77031, 106239, 142587, 156159, 216367, 230631, 410011, 511935, 626331, 837799
6Also, if anyone can find a number with a stopping distance of infinity (never reaches 1) I will give them the biggest bounty I can offer. =D – James – 2016-02-04T01:09:02.713
1So will many mathematicians... :P – Rɪᴋᴇʀ – 2016-02-04T01:11:44.720
Related: http://codegolf.stackexchange.com/q/12177/3808
– Doorknob – 2016-02-04T01:12:16.3105This is only conjecture, but I suspect that rule 2 is a mathematical fact rather than just a challenge restriction. – trichoplax – 2016-02-04T01:14:28.367
1"You must take a number n as input, and output out all the Super Collatz numbers up to n" So if I understand this correctly you do NOT ask to output the first n super collatz number? Because that's what the Pyth answer does for example, so I think it's not clear enough. – Fatalize – 2016-02-04T07:52:46.740
@Fatalize Yes, that's correct. I'll make that a little more clear in the post. – James – 2016-02-04T08:09:02.283
What? No answer with a Mathematica built in? – Billy S – 2017-07-16T19:26:42.667