Avatar of Dalakos43

Dalakos43's solution

to Difference Of Squares in the C Track

Published at Aug 29 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();
}

src/difference_of_squares.c

#include "./difference_of_squares.h"

int square_of_sum(int number)
{
    int result=0;
    for (int i=1; i <= number; i++) result += i;
    return (result*result);
}

int sum_of_squares(int number)
{
    int result=0;
    for (int i=1; i <= number; i++) result += (i * i);
    return result;
}

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

src/difference_of_squares.h

#ifndef DIFFERENCE_OF_SQUARES_H
#define DIFFERENCE_OF_SQUARES_H

int square_of_sum(int number);
int sum_of_squares(int number);
int difference_of_squares(int number);

#endif // DIFFERENCE_OF_SQUARES_H

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?