diff options
Diffstat (limited to 'c/difference-of-squares/src')
-rw-r--r-- | c/difference-of-squares/src/difference_of_squares.c | 19 | ||||
-rw-r--r-- | c/difference-of-squares/src/difference_of_squares.h | 8 |
2 files changed, 27 insertions, 0 deletions
diff --git a/c/difference-of-squares/src/difference_of_squares.c b/c/difference-of-squares/src/difference_of_squares.c new file mode 100644 index 0000000..0d9b6b2 --- /dev/null +++ b/c/difference-of-squares/src/difference_of_squares.c @@ -0,0 +1,19 @@ +#include "difference_of_squares.h" + +unsigned int sum_of_squares(unsigned int number) { + unsigned int sum = 0; + for (unsigned int idx = 1; idx <= number; idx++) + sum += (idx * idx); + return sum; +} + +unsigned int square_of_sum(unsigned int number) { + unsigned int sum = 0; + for (unsigned int idx = 1; idx <= number; idx++) + sum += idx; + return sum * sum; +} + +unsigned int difference_of_squares(unsigned int number) { + return square_of_sum(number) - sum_of_squares(number); +} diff --git a/c/difference-of-squares/src/difference_of_squares.h b/c/difference-of-squares/src/difference_of_squares.h new file mode 100644 index 0000000..cbb65f2 --- /dev/null +++ b/c/difference-of-squares/src/difference_of_squares.h @@ -0,0 +1,8 @@ +#ifndef DIFFERENCE_OF_SQUARES_H +#define DIFFERENCE_OF_SQUARES_H + +unsigned int sum_of_squares(unsigned int number); +unsigned int square_of_sum(unsigned int number); +unsigned int difference_of_squares(unsigned int number); + +#endif |