How to resolve the algorithm Anti-primes step by step in the Elixir programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Anti-primes step by step in the Elixir programming language
Table of Contents
Problem Statement
The anti-primes (or highly composite numbers, sequence A002182 in the OEIS) are the natural numbers with more factors than any smaller than itself.
Generate and show here, the first twenty anti-primes.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Anti-primes step by step in the Elixir programming language
Source code in the elixir programming language
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
You may also check:How to resolve the algorithm Next highest int from digits step by step in the Java programming language
You may also check:How to resolve the algorithm Count in octal step by step in the Raku programming language
You may also check:How to resolve the algorithm Draw a rotating cube step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Alore programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the Standard ML programming language