Avatar of vlzware

vlzware's solution

to Perfect Numbers in the C Track

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

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

Getting Started

Make sure you have read the C page 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)
{
}

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

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));
}

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

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

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

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

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

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

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

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

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

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

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);

   UnityEnd();
   return 0;
}

makefile

CFLAGS  = -std=c99
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -pedantic
CFLAGS += -Werror

VFLAGS  = --quiet
VFLAGS += --tool=memcheck
VFLAGS += --leak-check=full
VFLAGS += --error-exitcode=1

test: tests.out
	@./tests.out
memcheck: tests.out
	@valgrind $(VFLAGS) ./tests.out
	@echo "Memory check passed"

clean:
	rm -rf *.o *.out *.out.dSYM

tests.out: test/test_perfect_numbers.c src/perfect_numbers.c src/perfect_numbers.h
	@echo Compiling $@
	@cc $(CFLAGS) src/perfect_numbers.c test/vendor/unity.c test/test_perfect_numbers.c -o tests.out -lm

src/perfect_numbers.c

#include "perfect_numbers.h"
#include <math.h>

int aliquot(int n)
{
	if (n < 2)
		return 0;
	if (n < 4)
		return 1;

	int res = 0;
	int i;
	int up = (int) sqrt(n);
	for (i = 2; i <= up; i++)
		if (n % i == 0)
			res += (i == n/i)? i : (i + n/i);

	return res + 1;
}

kind classify_number(int n)
{
	if (n <= 0)
		return ERROR;

	int aq = aliquot(n);

	if (aq == n)
		return PERFECT_NUMBER;
	if (aq > n)
		return ABUNDANT_NUMBER;
	return DEFICIENT_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);

#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?