Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Hamming-numbers/Erlang/hamming-numbers-1.erl
Normal file
19
Task/Hamming-numbers/Erlang/hamming-numbers-1.erl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
list(N) -> array:to_list(element(1, array(N, [2, 3, 5]))).
|
||||
|
||||
nth(N) -> array:get(N-1, element(1, array(N, [2, 3, 5]))).
|
||||
|
||||
array(N, Primes) -> array(array:new(), N, 1, [{P, 1, P} || P <- Primes]).
|
||||
|
||||
array(Array, Max, Max, Candidates) -> {Array, Candidates};
|
||||
array(Array, Max, I, Candidates) ->
|
||||
Smallest = smallest(Candidates),
|
||||
N_array = array:set(I, Smallest, Array),
|
||||
array(N_array, Max, I+1, update(Smallest, N_array, Candidates)).
|
||||
|
||||
update(Val, Array, Candidates) -> [update_(Val, C, Array) || C <- Candidates].
|
||||
|
||||
update_(Val, {Val, Ind, Mul}, Array) ->
|
||||
{Mul*array:get(Ind, Array), Ind+1, Mul};
|
||||
update_(_, X, _) -> X.
|
||||
|
||||
smallest(L) -> lists:min([element(1, V) || V <- L]).
|
||||
18
Task/Hamming-numbers/Erlang/hamming-numbers-2.erl
Normal file
18
Task/Hamming-numbers/Erlang/hamming-numbers-2.erl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
nth(N, Batch) ->
|
||||
array:get(N-1, element(1, compact_array(N, Batch, [2, 3, 5]))).
|
||||
|
||||
compact_array(Goal, Lim, Primes) ->
|
||||
{Array, Candidates} = array(Lim, Primes),
|
||||
compact_array(Goal, Lim, Lim, Array, Candidates).
|
||||
|
||||
compact_array(Goal, _, Index, Array, Candidates) when Index > Goal ->
|
||||
{Array, Candidates};
|
||||
compact_array(Goal, Lim, Index, Array, Candidates) ->
|
||||
{N_array, N_candidates} =
|
||||
array(compact(Array, Candidates), Index + Lim, Index, Candidates),
|
||||
compact_array(Goal, Lim, Index+Lim, N_array, N_candidates).
|
||||
|
||||
compact(Array, L) ->
|
||||
Index = lists:min([element(2, V) || V <- L]),
|
||||
Keep = [E || E <- array:sparse_to_orddict(Array), element(1, E) >= Index],
|
||||
array:from_orddict(Keep).
|
||||
Loading…
Add table
Add a link
Reference in a new issue