Skip to content

Instantly share code, notes, and snippets.

@geoff-kruss
Created January 17, 2012 12:08
Show Gist options
  • Save geoff-kruss/1626434 to your computer and use it in GitHub Desktop.
Save geoff-kruss/1626434 to your computer and use it in GitHub Desktop.
erlang hailstone
-module(hailstone).
-import(io).
-export([main/1]).
hailstone(1) -> [1];
hailstone(N) when N band 1 == 1 -> [N|hailstone(N * 3 + 1)];
hailstone(N) when N band 1 == 0 -> [N|hailstone(N div 2)].
main(End) ->
F = fun (N) -> length(hailstone(N)) end,
Lengths = lists:map(F, lists:seq(1, End)),
average(Lengths).
average(List) ->
lists:sum(List) / length(List).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment