A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
24
Task/Hailstone-sequence/Erlang/hailstone-sequence.erl
Normal file
24
Task/Hailstone-sequence/Erlang/hailstone-sequence.erl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
-module(hailstone).
|
||||
-import(io).
|
||||
-export([main/0]).
|
||||
|
||||
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)].
|
||||
|
||||
max_length(Start, Stop) ->
|
||||
F = fun (N) -> {length(hailstone(N)), N} end,
|
||||
Lengths = lists:map(F, lists:seq(Start, Stop)),
|
||||
lists:max(Lengths).
|
||||
|
||||
main() ->
|
||||
io:format("hailstone(4): ~w~n", [hailstone(4)]),
|
||||
Seq27 = hailstone(27),
|
||||
io:format("hailstone(27) length: ~B~n", [length(Seq27)]),
|
||||
io:format("hailstone(27) first 4: ~w~n",
|
||||
[lists:sublist(Seq27, 4)]),
|
||||
io:format("hailstone(27) last 4: ~w~n",
|
||||
[lists:nthtail(length(Seq27) - 4, Seq27)]),
|
||||
io:format("finding maximum hailstone(N) length for 1 <= N <= 100000..."),
|
||||
{Length, N} = max_length(1, 100000),
|
||||
io:format(" done.~nhailstone(~B) length: ~B~n", [N, Length]).
|
||||
Loading…
Add table
Add a link
Reference in a new issue