Skip to content

Instantly share code, notes, and snippets.

@alyx
Created February 24, 2013 17:55
Show Gist options
  • Save alyx/5024824 to your computer and use it in GitHub Desktop.
Save alyx/5024824 to your computer and use it in GitHub Desktop.
-module(collatz).
-export([find_len/2]).
find_len(Start, Len) when Start =:= 1 orelse Start =:= 0 ->
Len;
find_len(Start, Len) when Start rem 2 =:= 1 ->
find_len((Start * 3) + 1, Len + 1);
find_len(Start, Len) when Start rem 2 =:= 0 ->
find_len((Start div 2), Len + 1).
find_longest(First, Last, Longest) when First < Last ->
Longest;
find_longest(First, Last, Longest) ->
Len = find_len(First, 1),
case Len of
{_, Len} when {_, Len} > Longest ->
find_longest(First - 1, Last, {First, Len});
_ ->
find_longest(First - 1, Last, Longest)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment