 # JaeHyoLee's solution

## to Perfect Numbers in the Lua Track

Published at Jul 13 2018 · 2 comments
Instructions
Test suite
Solution

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

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.

## Running the tests

To run the tests, run the command `busted` from within the exercise directory.

## Further information

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.

## Source

Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### perfect-numbers_spec.lua

``````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 }`````` 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 '

### What can you learn from this solution?

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.

• What compromises have been made?