ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Jul 13 2018
·
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 C page 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)
{
}
void test_square_of_sum_up_to_5(void)
{
TEST_ASSERT_EQUAL(225, square_of_sum(5));
}
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));
}
void test_square_of_sum_up_to_100(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(25502500, square_of_sum(100));
}
void test_sum_of_squares_up_to_5(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(55, sum_of_squares(5));
}
void test_sum_of_squares_up_to_10(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(385, sum_of_squares(10));
}
void test_sum_of_squares_up_to_100(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(338350, sum_of_squares(100));
}
void test_difference_of_squares_up_to_0(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(0, difference_of_squares(0));
}
void test_difference_of_squares_up_to_5(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(170, difference_of_squares(5));
}
void test_difference_of_squares_up_to_10(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(2640, difference_of_squares(10));
}
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);
UnityEnd();
return 0;
}
```

```
#include "difference_of_squares.h"
int square_of_sum(const int n)
{
/* https://en.wikipedia.org/wiki/Triangular_number */
int trnum = n * (n + 1) / 2;
return (trnum * trnum);
}
int sum_of_squares(const int n)
{
/* https://en.wikipedia.org/wiki/Square_pyramidal_number */
return (n * (n + 1) * (2*n + 1) / 6);
}
int difference_of_squares(const int n)
{
return (square_of_sum(n) - sum_of_squares(n));
}
```

```
#ifndef DIFFSQUARES_H
#define DIFFSQUARES_H
int square_of_sum(const int n);
int sum_of_squares(const int n);
int difference_of_squares(const int n);
#endif
```

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,423 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments