🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of Der-Prompt-Fuhrer

Der-Prompt-Fuhrer's solution

to Perfect Numbers in the C Track

Published at Jan 23 2021 · 0 comments
Instructions
Test suite
Solution

Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.

The Greek mathematician Nicomachus devised a classification scheme for positive integers, 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.

Getting Started

Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

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.

test_perfect_numbers.c

#include "vendor/unity.h"
#include "../src/perfect_numbers.h"

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_smallest_perfect_number_is_classified_correctly(void)
{
   TEST_ASSERT_EQUAL(PERFECT_NUMBER, classify_number(6));
}

static void test_medium_perfect_number_is_classified_correctly(void)
{
   TEST_IGNORE();               // delete this line to run test
   TEST_ASSERT_EQUAL(PERFECT_NUMBER, classify_number(28));
}

static void test_large_perfect_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(PERFECT_NUMBER, classify_number(33550336));
}

static void test_smallest_abundant_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ABUNDANT_NUMBER, classify_number(12));
}

static void test_medium_abundant_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ABUNDANT_NUMBER, classify_number(30));
}

static void test_large_abundant_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ABUNDANT_NUMBER, classify_number(33550335));
}

static void test_smallest_prime_deficient_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(2));
}

static void
test_smallest_non_prime_deficient_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(4));
}

static void test_medium_deficient_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(32));
}

static void test_large_deficient_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(33550337));
}

static void test_edge_case_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(1));
}

static void test_zero_is_rejected(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ERROR, classify_number(0));
}

static void test_negative_integer_is_rejected(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ERROR, classify_number(-1));
}

int main(void)
{
   UnityBegin("test/test_perfect_numbers.c");

   RUN_TEST(test_smallest_perfect_number_is_classified_correctly);
   RUN_TEST(test_medium_perfect_number_is_classified_correctly);
   RUN_TEST(test_large_perfect_number_is_classified_correctly);
   RUN_TEST(test_smallest_abundant_number_is_classified_correctly);
   RUN_TEST(test_medium_abundant_number_is_classified_correctly);
   RUN_TEST(test_large_abundant_number_is_classified_correctly);
   RUN_TEST(test_smallest_prime_deficient_number_is_classified_correctly);
   RUN_TEST(test_smallest_non_prime_deficient_number_is_classified_correctly);
   RUN_TEST(test_medium_deficient_number_is_classified_correctly);
   RUN_TEST(test_large_deficient_number_is_classified_correctly);
   RUN_TEST(test_edge_case_is_classified_correctly);
   RUN_TEST(test_zero_is_rejected);
   RUN_TEST(test_negative_integer_is_rejected);

   return UnityEnd();
}

src/perfect_numbers.c

#include "perfect_numbers.h"

#define INIT_SIZE 10


int sum_divisors(int n) {
    int sum = 0;

    for(int i = 1; i <= n / 2; i++) {
        if(n % i == 0) {
            sum += i;
        }
    }
    return sum;
}

kind classify_number(int n) {

    int sum = sum_divisors(n);

    if(n <= 0){
        return ERROR;
    } else if(n == sum) {
        return PERFECT_NUMBER;
    } else if(n > sum) {
        return DEFICIENT_NUMBER;
    } else {
        return ABUNDANT_NUMBER;
    }
}

src/perfect_numbers.h

#ifndef PERFECT_NUMBERS_H
#define PERFECT_NUMBERS_H

typedef enum {
   PERFECT_NUMBER = 1,
   ABUNDANT_NUMBER = 2,
   DEFICIENT_NUMBER = 3,
   ERROR = -1
} kind;

kind classify_number(int n);
int sum_divisors(int n);

#endif

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?
  • Are there new concepts here that you could read more about to improve your understanding?