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
Implement a way to determine whether a given number is perfect. Depending on your language track, you may also need to implement a way to determine whether a given number is abundant or deficient.
To run the tests, run the command busted
from within the exercise directory.
For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.
Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
local perfect_numbers = require('perfect-numbers')
describe('perfect-numbers', function()
it('should be able to calculate the Aliquot sum of a number with no divisors', function()
assert.equal(0, perfect_numbers.aliquot_sum(1))
end)
it('should be able to calculate the Aliquot sum of a number with a single divisor', function()
assert.equal(1, perfect_numbers.aliquot_sum(2))
end)
it('should be able to calculate the Aliquot sum of a number with a multiple divisors', function()
assert.equal(15, perfect_numbers.aliquot_sum(16))
end)
it('should be able to calculate the Aliquot sum of a large number', function()
assert.equal(229, perfect_numbers.aliquot_sum(1115))
end)
it('should classify numbers whose Aliquot sum is less than itself as deficient', function()
assert.equal('deficient', perfect_numbers.classify(13))
end)
it('should classify numbers whose Aliquot sum is equal to itself as perfect', function()
assert.equal('perfect', perfect_numbers.classify(28))
end)
it('should classify numbers whose Aliquot sum is greater than itself as abundant', function()
assert.equal('abundant', perfect_numbers.classify(12))
end)
end)
local function aliquot_sum(number)
local sum = 1
local to = math.sqrt(number)
for from = 2, to do
if number % from == 0 then sum = sum + from + (from ~= (number/from) and number/from or 0) end
end
return (number == 1 and 0 or sum)
end
local function classify(number)
local factors = aliquot_sum(number)
return (number == factors and 'perfect' or number > factors and 'deficient' or 'abundant')
end
return { aliquot_sum = aliquot_sum, classify = classify }
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,276 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments
I would consider simplifying this for from = 2, to do if number % from == 0 then sum = sum + from + (from ~= (number/from) and number/from or 0) end end
to for from = 2, to - 1 do if number % from == 0 then sum = sum + from end end
While I appreciate the x and y or z construct, it is not really needed :)
@ryanplusplus That's good suggestion. but unfortunately.. it wouldn't work. in my logic.. 'to' is sqrt(n) .. so I could shorten the for loops. so.. when I calculate aliquot of 'n'... if number % from == 0 it means I can get two aliquot from this. let's say n = 100 and 100 % 2 == 0 -> means 2 and 100/2 = 50 are aliquot.. And run for loop till sqrt(100) -> which is 10. That is why I needed other conditions instead of 'sum = sum + from '