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.
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
raise Exception
, you should write:
raise Exception("Meaningful message indicating the source of the error")
To run the tests, run the appropriate command below (why they are different):
py.test perfect_numbers_test.py
pytest perfect_numbers_test.py
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
python -m pytest perfect_numbers_test.py
pytest
options-v
: enable verbose output-x
: stop running tests on first failure--ff
: run failures from previous test before running other test casesFor other options, see python -m pytest -h
Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/python/perfect-numbers
directory.
You can find your Exercism workspace by running exercism debug
and looking for the line that starts with Workspace
.
For more detailed information about running tests, code style and linting, please see the help 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.
import unittest
from perfect_numbers import classify
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
class PerfectNumbersTest(unittest.TestCase):
def test_smallest_perfect_number(self):
self.assertIs(classify(6), "perfect")
def test_medium_perfect_number(self):
self.assertIs(classify(28), "perfect")
def test_large_perfect_number(self):
self.assertIs(classify(33550336), "perfect")
class AbundantNumbersTest(unittest.TestCase):
def test_smallest_abundant_number(self):
self.assertIs(classify(12), "abundant")
def test_medium_abundant_number(self):
self.assertIs(classify(30), "abundant")
def test_large_abundant_number(self):
self.assertIs(classify(33550335), "abundant")
class DeficientNumbersTest(unittest.TestCase):
def test_smallest_prime_deficient_number(self):
self.assertIs(classify(2), "deficient")
def test_smallest_nonprime_deficient_number(self):
self.assertIs(classify(4), "deficient")
def test_medium_deficient_number(self):
self.assertIs(classify(32), "deficient")
def test_large_deficient_number(self):
self.assertIs(classify(33550337), "deficient")
def test_edge_case(self):
self.assertIs(classify(1), "deficient")
class InvalidInputsTest(unittest.TestCase):
def test_zero(self):
with self.assertRaisesWithMessage(ValueError):
classify(0)
def test_negative(self):
with self.assertRaisesWithMessage(ValueError):
classify(-1)
# Utility functions
def setUp(self):
try:
self.assertRaisesRegex
except AttributeError:
self.assertRaisesRegex = self.assertRaisesRegexp
def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")
if __name__ == '__main__':
unittest.main()
import math
def is_perfect(number):
return sum(factors(number)) == number
def factors(n):
return set(reduce(list.__add__, pairs_of_factors(n))) - set([n])
def pairs_of_factors(n):
return [[i, n / i] for i in xrange(1, int(math.sqrt(n)) + 1) if n % i == 0]
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,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments