🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # vlzware's solution

## to Difference Of Squares 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.

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

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

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

### src/difference_of_squares.c

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

### src/difference_of_squares.h

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