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.
For installation and learning resources, refer to the Ruby resources page.
For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:
gem install minitest
If you would like color output, you can require 'minitest/pride'
in
the test file, or note the alternative instruction, below, for running
the test file.
Run the tests from the exercise directory using the following command:
ruby perfect_numbers_test.rb
To include color from the command line:
ruby -r minitest/pride perfect_numbers_test.rb
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.
require 'minitest/autorun'
require_relative 'perfect_numbers'
class PerfectNumberTest < Minitest::Test
def test_initialize_perfect_number
assert_raises RuntimeError do
PerfectNumber.classify(-1)
end
end
def test_classify_deficient
assert_equal 'deficient', PerfectNumber.classify(13)
end
def test_classify_perfect
assert_equal 'perfect', PerfectNumber.classify(28)
end
def test_classify_abundant
assert_equal 'abundant', PerfectNumber.classify(12)
end
end
# frozen_string_literal: true
module PerfectNumber
EQUAL = 0
private_constant :EQUAL
GREATER = 1
private_constant :GREATER
module_function
def classify(number)
raise RuntimeError unless number.positive?
case aliquot_sum(number) <=> number
when GREATER
"abundant"
when EQUAL
"perfect"
else
"deficient"
end
end
def aliquot_sum(number)
(1...number)
.each
.with_object(number)
.each_with_object([], &method(:add_factor))
.sum
end
private_class_method :aliquot_sum
def add_factor((candidate_factor, number), acc)
acc << candidate_factor if factor?(candidate_factor, number)
end
private_class_method :add_factor
def factor?(candidate_factor, number)
number
.modulo(candidate_factor)
.zero?
end
private_class_method :factor?
end
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,081 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