2014-09-11

# Problem 014: Longest Collatz sequence

Description:

The following iterative sequence is defined for the set of positive integers:

n -> n/2 (n is even) n -> 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence: 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

Solution:
v               >3*1+v
>0::p3>1+:1\>:2%|    >\1+\:1- #v_\$:00g\`#v_00p:10pv
v:0.g0 1<   ^  \$># 2/^#1       <         \$
g     ^_^#!`  **"}}@":                   <        <
>": ",,.@
Start
??
Pause
Reset
Output:
Stack:   (0)

Explanation:

Again a simple problem, we get the length of every sequence (= until they reach `1`) and print the highest.

 Interpreter steps: 3 877 209 672 Execution time (BefunExec): 11min (5.40 MHz) Program size: 51 x 5 (fully conform befunge-93) Solution: 837799 Solved at: 2014-09-11

made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript