Published at Sep 05 2019
·
0 comments

Instructions

Test suite

Solution

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is (1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is 1² + 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.

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.

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.

Problem 6 at Project Euler http://projecteuler.net/problem=6

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

```
#include "vendor/unity.h"
#include "../src/difference_of_squares.h"
void setUp(void)
{
}
void tearDown(void)
{
}
static void test_square_of_sum_up_to_5(void)
{
TEST_ASSERT_EQUAL(225, square_of_sum(5));
}
static void test_square_of_sum_up_to_10(void)
{
TEST_IGNORE(); // delete this line to run test
TEST_ASSERT_EQUAL(3025, square_of_sum(10));
}
static void test_square_of_sum_up_to_100(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(25502500, square_of_sum(100));
}
static void test_sum_of_squares_up_to_5(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(55, sum_of_squares(5));
}
static void test_sum_of_squares_up_to_10(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(385, sum_of_squares(10));
}
static void test_sum_of_squares_up_to_100(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(338350, sum_of_squares(100));
}
static void test_difference_of_squares_up_to_0(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(0, difference_of_squares(0));
}
static void test_difference_of_squares_up_to_5(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(170, difference_of_squares(5));
}
static void test_difference_of_squares_up_to_10(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(2640, difference_of_squares(10));
}
static void test_difference_of_squares_up_to_100(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(25164150, difference_of_squares(100));
}
int main(void)
{
UnityBegin("test/test_difference_of_squares.c");
RUN_TEST(test_square_of_sum_up_to_5);
RUN_TEST(test_square_of_sum_up_to_10);
RUN_TEST(test_square_of_sum_up_to_100);
RUN_TEST(test_sum_of_squares_up_to_5);
RUN_TEST(test_sum_of_squares_up_to_10);
RUN_TEST(test_sum_of_squares_up_to_100);
RUN_TEST(test_difference_of_squares_up_to_0);
RUN_TEST(test_difference_of_squares_up_to_5);
RUN_TEST(test_difference_of_squares_up_to_10);
RUN_TEST(test_difference_of_squares_up_to_100);
return UnityEnd();
}
```

```
#include "difference_of_squares.h"
#include <math.h>
unsigned int sq(unsigned int a)
{
return a * a;
}
unsigned int sum_of_squares(unsigned int number)
{
unsigned int i = 0;
unsigned int s = 0;
for (i = 1; i <= number; i++)
{
s += sq(i);
}
return s;
}
unsigned int square_of_sum(unsigned int number)
{
unsigned int i = 0;
unsigned int s = 0;
for (i = 1; i <= number; i++)
{
s += i;
}
return sq(s);
}
unsigned int difference_of_squares(unsigned int number)
{
return square_of_sum(number) - sum_of_squares(number);
}
```

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?

Level up your programming skills with 3,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments