Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
31
Task/Anti-primes/Elixir/anti-primes.elixir
Normal file
31
Task/Anti-primes/Elixir/anti-primes.elixir
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
defmodule AntiPrimes do
|
||||
def divcount(n) when is_integer(n), do: divcount(n, 1, 0)
|
||||
|
||||
def divcount(n, d, count) when d * d > n, do: count
|
||||
def divcount(n, d, count) do
|
||||
divs = case rem(n, d) do
|
||||
0 ->
|
||||
case n - d * d do
|
||||
0 -> 1
|
||||
_ -> 2
|
||||
end
|
||||
_ -> 0
|
||||
end
|
||||
divcount(n, d + 1, count + divs)
|
||||
end
|
||||
|
||||
def antiprimes(n), do: antiprimes(n, 1, 0, [])
|
||||
|
||||
def antiprimes(0, _, _, l), do: Enum.reverse(l)
|
||||
def antiprimes(n, m, max, l) do
|
||||
count = divcount(m)
|
||||
case count > max do
|
||||
true -> antiprimes(n-1, m+1, count, [m|l])
|
||||
false -> antiprimes(n, m+1, max, l)
|
||||
end
|
||||
end
|
||||
|
||||
def main() do
|
||||
:io.format("The first 20 anti-primes are ~w~n", [antiprimes(20)])
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue