Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Hailstone-sequence/Erlang/hailstone-sequence-1.erl
Normal file
24
Task/Hailstone-sequence/Erlang/hailstone-sequence-1.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]).
|
||||
28
Task/Hailstone-sequence/Erlang/hailstone-sequence-2.erl
Normal file
28
Task/Hailstone-sequence/Erlang/hailstone-sequence-2.erl
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
-module(collatz).
|
||||
-export([main/0,collatz/1,coll/1,max_atz_under/1]).
|
||||
|
||||
collatz(1) -> 1;
|
||||
collatz(N) when N rem 2 == 0 -> 1 + collatz(N div 2);
|
||||
collatz(N) when N rem 2 > 0 -> 1 + collatz(3 * N +1).
|
||||
|
||||
max_atz_under(N) ->
|
||||
F = fun (X) -> {collatz(X), X} end,
|
||||
{_, Index} = lists:max(lists:map(F, lists:seq(1, N))),
|
||||
Index.
|
||||
|
||||
coll(1) -> [1];
|
||||
coll(N) when N rem 2 == 0 -> [N|coll(N div 2)];
|
||||
coll(N) -> [N|coll(3 * N + 1)].
|
||||
|
||||
main() ->
|
||||
io:format("collatz(4) non-list total: ~w~n", [collatz(4)]),
|
||||
io:format("coll(4) with lists ~w~n", [coll(4)] ),
|
||||
Seq27 = coll(27),
|
||||
Seq1000 = coll(max_atz_under(100000)),
|
||||
io:format("coll(27) length: ~B~n", [length(Seq27)]),
|
||||
io:format("coll(27) first 4: ~w~n", [lists:sublist(Seq27, 4)]),
|
||||
io:format("collatz(27) last 4: ~w~n",
|
||||
[lists:nthtail(length(Seq27) - 4, Seq27)]),
|
||||
io:format("maximum N <= 100000..."),
|
||||
io:format("Max: ~w~n", [max_atz_under(100000)]),
|
||||
io:format("Total: ~w~n", [ length( Seq1000 ) ] ).
|
||||
Loading…
Add table
Add a link
Reference in a new issue