Implement Aliquot Sum and the Perfect Number algorithm
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.
The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9
Perfect: aliquot sum = number
6 is a perfect number because (1 + 2 + 3) = 6
28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
Abundant: aliquot sum > number
12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
Deficient: aliquot sum < number
8 is a deficient number because (1 + 2 + 4) = 7
Prime numbers are deficient
elixir
add a comment |
Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.
The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9
Perfect: aliquot sum = number
6 is a perfect number because (1 + 2 + 3) = 6
28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
Abundant: aliquot sum > number
12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
Deficient: aliquot sum < number
8 is a deficient number because (1 + 2 + 4) = 7
Prime numbers are deficient
elixir
2
I would suggest including some code, even if just partial and make the question more specific. Can you write or find code to generate the factors? That would seem to me to be the only tricky part. If not, do everything else, and then the question becomes, "How to generate factors in elixir?"
– zanerock
Nov 23 '18 at 16:11
2
Hi, welcome to Stack Overflow. The site is meant to help programmers, but that doesn't mean you can post (what appears to be) homework and expect us to solve it for you. Some tips: Don't post an entire exercise to solve. instead try to solve it, and if you get stuck, ask a question saying what you've tried, and why you're stuck (Do you get an error? Is the program not behaving as expected?)
– ldeld
Nov 23 '18 at 16:11
it is'nt home work im trying to learn elixir myself so that this exercice i find it on site of exercise
– yaovi antoine kodjo
Nov 23 '18 at 16:17
1
You will probably not find anyone that will solve that for you (this site is not for that). I will help you pointing a link of how to generate factors: rosettacode.org/wiki/Factors_of_an_integer#Elixir. I hope it helps you to at least start and come back with a better question.
– Marcos Tapajós
Nov 23 '18 at 16:34
add a comment |
Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.
The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9
Perfect: aliquot sum = number
6 is a perfect number because (1 + 2 + 3) = 6
28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
Abundant: aliquot sum > number
12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
Deficient: aliquot sum < number
8 is a deficient number because (1 + 2 + 4) = 7
Prime numbers are deficient
elixir
Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.
The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9
Perfect: aliquot sum = number
6 is a perfect number because (1 + 2 + 3) = 6
28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
Abundant: aliquot sum > number
12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
Deficient: aliquot sum < number
8 is a deficient number because (1 + 2 + 4) = 7
Prime numbers are deficient
elixir
elixir
edited Nov 25 '18 at 6:32
Sheharyar
46.7k12112165
46.7k12112165
asked Nov 23 '18 at 15:59
yaovi antoine kodjoyaovi antoine kodjo
11
11
2
I would suggest including some code, even if just partial and make the question more specific. Can you write or find code to generate the factors? That would seem to me to be the only tricky part. If not, do everything else, and then the question becomes, "How to generate factors in elixir?"
– zanerock
Nov 23 '18 at 16:11
2
Hi, welcome to Stack Overflow. The site is meant to help programmers, but that doesn't mean you can post (what appears to be) homework and expect us to solve it for you. Some tips: Don't post an entire exercise to solve. instead try to solve it, and if you get stuck, ask a question saying what you've tried, and why you're stuck (Do you get an error? Is the program not behaving as expected?)
– ldeld
Nov 23 '18 at 16:11
it is'nt home work im trying to learn elixir myself so that this exercice i find it on site of exercise
– yaovi antoine kodjo
Nov 23 '18 at 16:17
1
You will probably not find anyone that will solve that for you (this site is not for that). I will help you pointing a link of how to generate factors: rosettacode.org/wiki/Factors_of_an_integer#Elixir. I hope it helps you to at least start and come back with a better question.
– Marcos Tapajós
Nov 23 '18 at 16:34
add a comment |
2
I would suggest including some code, even if just partial and make the question more specific. Can you write or find code to generate the factors? That would seem to me to be the only tricky part. If not, do everything else, and then the question becomes, "How to generate factors in elixir?"
– zanerock
Nov 23 '18 at 16:11
2
Hi, welcome to Stack Overflow. The site is meant to help programmers, but that doesn't mean you can post (what appears to be) homework and expect us to solve it for you. Some tips: Don't post an entire exercise to solve. instead try to solve it, and if you get stuck, ask a question saying what you've tried, and why you're stuck (Do you get an error? Is the program not behaving as expected?)
– ldeld
Nov 23 '18 at 16:11
it is'nt home work im trying to learn elixir myself so that this exercice i find it on site of exercise
– yaovi antoine kodjo
Nov 23 '18 at 16:17
1
You will probably not find anyone that will solve that for you (this site is not for that). I will help you pointing a link of how to generate factors: rosettacode.org/wiki/Factors_of_an_integer#Elixir. I hope it helps you to at least start and come back with a better question.
– Marcos Tapajós
Nov 23 '18 at 16:34
2
2
I would suggest including some code, even if just partial and make the question more specific. Can you write or find code to generate the factors? That would seem to me to be the only tricky part. If not, do everything else, and then the question becomes, "How to generate factors in elixir?"
– zanerock
Nov 23 '18 at 16:11
I would suggest including some code, even if just partial and make the question more specific. Can you write or find code to generate the factors? That would seem to me to be the only tricky part. If not, do everything else, and then the question becomes, "How to generate factors in elixir?"
– zanerock
Nov 23 '18 at 16:11
2
2
Hi, welcome to Stack Overflow. The site is meant to help programmers, but that doesn't mean you can post (what appears to be) homework and expect us to solve it for you. Some tips: Don't post an entire exercise to solve. instead try to solve it, and if you get stuck, ask a question saying what you've tried, and why you're stuck (Do you get an error? Is the program not behaving as expected?)
– ldeld
Nov 23 '18 at 16:11
Hi, welcome to Stack Overflow. The site is meant to help programmers, but that doesn't mean you can post (what appears to be) homework and expect us to solve it for you. Some tips: Don't post an entire exercise to solve. instead try to solve it, and if you get stuck, ask a question saying what you've tried, and why you're stuck (Do you get an error? Is the program not behaving as expected?)
– ldeld
Nov 23 '18 at 16:11
it is'nt home work im trying to learn elixir myself so that this exercice i find it on site of exercise
– yaovi antoine kodjo
Nov 23 '18 at 16:17
it is'nt home work im trying to learn elixir myself so that this exercice i find it on site of exercise
– yaovi antoine kodjo
Nov 23 '18 at 16:17
1
1
You will probably not find anyone that will solve that for you (this site is not for that). I will help you pointing a link of how to generate factors: rosettacode.org/wiki/Factors_of_an_integer#Elixir. I hope it helps you to at least start and come back with a better question.
– Marcos Tapajós
Nov 23 '18 at 16:34
You will probably not find anyone that will solve that for you (this site is not for that). I will help you pointing a link of how to generate factors: rosettacode.org/wiki/Factors_of_an_integer#Elixir. I hope it helps you to at least start and come back with a better question.
– Marcos Tapajós
Nov 23 '18 at 16:34
add a comment |
1 Answer
1
active
oldest
votes
Here's an implementation to get you started, though it'll only work for positive integers and could use some improvements to boost performance:
defmodule PerfectNumber do
def check(n) do
sum = aliquot_sum(n)
cond do
sum == n -> :perfect
sum < n -> :deficient
sum > n -> :abundant
end
end
def aliquot_sum(n) do
Enum.sum(factors(n))
end
def factors(1), do: [1]
def factors(n) do
for i <- 1..div(n,2), rem(n,i) == 0, do: i
end
end
Works as expected:
iex> PerfectNumber.check(6)
# => :perfect
iex> PerfectNumber.check(12)
# => :abundant
iex> PerfectNumber.check(8)
# => :deficient
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449738%2fimplement-aliquot-sum-and-the-perfect-number-algorithm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's an implementation to get you started, though it'll only work for positive integers and could use some improvements to boost performance:
defmodule PerfectNumber do
def check(n) do
sum = aliquot_sum(n)
cond do
sum == n -> :perfect
sum < n -> :deficient
sum > n -> :abundant
end
end
def aliquot_sum(n) do
Enum.sum(factors(n))
end
def factors(1), do: [1]
def factors(n) do
for i <- 1..div(n,2), rem(n,i) == 0, do: i
end
end
Works as expected:
iex> PerfectNumber.check(6)
# => :perfect
iex> PerfectNumber.check(12)
# => :abundant
iex> PerfectNumber.check(8)
# => :deficient
add a comment |
Here's an implementation to get you started, though it'll only work for positive integers and could use some improvements to boost performance:
defmodule PerfectNumber do
def check(n) do
sum = aliquot_sum(n)
cond do
sum == n -> :perfect
sum < n -> :deficient
sum > n -> :abundant
end
end
def aliquot_sum(n) do
Enum.sum(factors(n))
end
def factors(1), do: [1]
def factors(n) do
for i <- 1..div(n,2), rem(n,i) == 0, do: i
end
end
Works as expected:
iex> PerfectNumber.check(6)
# => :perfect
iex> PerfectNumber.check(12)
# => :abundant
iex> PerfectNumber.check(8)
# => :deficient
add a comment |
Here's an implementation to get you started, though it'll only work for positive integers and could use some improvements to boost performance:
defmodule PerfectNumber do
def check(n) do
sum = aliquot_sum(n)
cond do
sum == n -> :perfect
sum < n -> :deficient
sum > n -> :abundant
end
end
def aliquot_sum(n) do
Enum.sum(factors(n))
end
def factors(1), do: [1]
def factors(n) do
for i <- 1..div(n,2), rem(n,i) == 0, do: i
end
end
Works as expected:
iex> PerfectNumber.check(6)
# => :perfect
iex> PerfectNumber.check(12)
# => :abundant
iex> PerfectNumber.check(8)
# => :deficient
Here's an implementation to get you started, though it'll only work for positive integers and could use some improvements to boost performance:
defmodule PerfectNumber do
def check(n) do
sum = aliquot_sum(n)
cond do
sum == n -> :perfect
sum < n -> :deficient
sum > n -> :abundant
end
end
def aliquot_sum(n) do
Enum.sum(factors(n))
end
def factors(1), do: [1]
def factors(n) do
for i <- 1..div(n,2), rem(n,i) == 0, do: i
end
end
Works as expected:
iex> PerfectNumber.check(6)
# => :perfect
iex> PerfectNumber.check(12)
# => :abundant
iex> PerfectNumber.check(8)
# => :deficient
edited Nov 25 '18 at 6:33
answered Nov 24 '18 at 6:36
SheharyarSheharyar
46.7k12112165
46.7k12112165
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449738%2fimplement-aliquot-sum-and-the-perfect-number-algorithm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I would suggest including some code, even if just partial and make the question more specific. Can you write or find code to generate the factors? That would seem to me to be the only tricky part. If not, do everything else, and then the question becomes, "How to generate factors in elixir?"
– zanerock
Nov 23 '18 at 16:11
2
Hi, welcome to Stack Overflow. The site is meant to help programmers, but that doesn't mean you can post (what appears to be) homework and expect us to solve it for you. Some tips: Don't post an entire exercise to solve. instead try to solve it, and if you get stuck, ask a question saying what you've tried, and why you're stuck (Do you get an error? Is the program not behaving as expected?)
– ldeld
Nov 23 '18 at 16:11
it is'nt home work im trying to learn elixir myself so that this exercice i find it on site of exercise
– yaovi antoine kodjo
Nov 23 '18 at 16:17
1
You will probably not find anyone that will solve that for you (this site is not for that). I will help you pointing a link of how to generate factors: rosettacode.org/wiki/Factors_of_an_integer#Elixir. I hope it helps you to at least start and come back with a better question.
– Marcos Tapajós
Nov 23 '18 at 16:34