 # preri's solution

## to Difference Of Squares in the C Track

Published at Sep 26 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.

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

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

## Submitting Incomplete Solutions

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

### test_difference_of_squares.c

``````#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 <stdio.h>
#include <math.h>

unsigned int square_of_sum(unsigned int number) {
int sum = 0;
for (unsigned int i = 1; i <= number; i++) sum += i;
return pow(sum, 2);
}

unsigned int sum_of_squares(unsigned int number) {
int sum = 0;
for (unsigned int i = 1; i <= number; i++) sum += pow(i, 2);
return sum;
}

unsigned int difference_of_squares(unsigned int number) {
return square_of_sum(number) - sum_of_squares(number);
}``````