Erlang Count Non Divisible
-module(count_non_divisible).
-export([count_non_divisible/1]).
count_non_divisible(A) ->
Size = length(A),
Occ = lists:foldl(fun(V, Map) ->
maps:update_with(V, fun(C) -> C + 1 end, 1, Map)
end, #{}, A),
[Size - count_divisors(V, Occ) || V <- A].
count_divisors(V, Occ) ->
count_divisors(V, Occ, 1, 0).
count_divisors(V, _Occ, I, Count) when I * I > V ->
Count;
count_divisors(V, Occ, I, Count) ->
case V rem I of
0 ->
Base = Count + maps:get(I, Occ, 0),
Total = case V div I =:= I of
true -> Base;
false -> Base + maps:get(V div I, Occ, 0)
end,
count_divisors(V, Occ, I + 1, Total);
_ ->
count_divisors(V, Occ, I + 1, Count)
end.
This counts how often each value appears, then subtracts the divisor matches so you get the non-divisible count for each item.