initial commit

This commit is contained in:
2021-08-08 21:11:22 +02:00
commit fe7136d801
130 changed files with 6858 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
#include "difference_of_squares.h"
/* See GNUmakefile in following link for explanation
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
*/
#if defined UNIT_TEST || defined DEBUG
#include <stdio.h>
#include <stdlib.h>
#endif
unsigned int sum_of_squares(unsigned int number)
{
/* the sum of 1st n integers squares is:
* S = 1² + 2² + 3² ..... + (n-1)² + n²
* = [ n * (n+1) * (2n+1) ] / 6
* some visual explanations on:
* http://www.takayaiwamoto.com/Sums_and_Series/sumsqr_1.html
*/
return number * (number+1) * (2*number + 1) / 6;
}
unsigned int square_of_sum(unsigned int number)
{
register int res;
/* The sum of n 1st integers is:
* S = 1 + 2 + 3 ... + (n-1) + n
* = [ n * (n+1) ] / 2
* demonstration is trivial for this one.
*/
res=number * (number+1) / 2;
return res*res;
}
unsigned int difference_of_squares(unsigned int number)
{
return square_of_sum(number) - sum_of_squares(number);
}
#ifdef UNIT_TEST
int main(int ac, char **av)
{
int arg=1;
int i;
for (; arg<ac; ++arg) {
i=atoi(av[arg]);
printf("sumsq(%d)=%d\n", i, sum_of_squares(i));
printf("sqsum(%d)=%d\n", i, square_of_sum(i));
printf("diff(%d)=%d\n", i, difference_of_squares(i));
}
}
#endif

View File

@@ -0,0 +1,16 @@
#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);
/* See GNUmakefile in following link for explanation
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
*/
#ifdef TESTALL
#undef TEST_IGNORE
#define TEST_IGNORE() {}
#endif
#endif