# ShareMyScreen/ProjectEuler/0014

Jump to navigation
Jump to search

What is the longest Collatz sequence for a natural number under 1e6?

Members of a sequence as derived as follows: if the number is odd, multiply it by 3 and add 1; if odd, halve it.

(i.>. /)(($: @ ((-: ` ([:>:3*]) @. (2&|) @ {.) , >:@{:)) ` {: @. (1&=@{.)"1)M.(i.1e6),.1 (i.1e6-1),.1 NB. 1 through 999999 in a column, next to a column of 1s; the latter will be our state, that is, the length of the sequence so far ] M. NB. highly recursive verbs' performance may benefit from memoization NB. recurse with the result NB. │ else, add one to the legth to the tail and perform the arithmatic on the head, and combine them NB. │ │ give the tail, that is, the recorded length of the sequence NB. │ │ │ if the sequence has ended (assuming it always does at 1) NB. │ │ │ │ for each row NB. │_ │_____________________________________________ │_ │_______ _ │_ (($: @ ((-: ` ([: >: 3 * ]) @. (2&|) @ {.) , >: @ {:) ) ` {: @. (1&= @ {.) "1) (i. >./) NB. gives the index of greatest number