summary refs log tree commit diff stats
path: root/c/difference-of-squares/src/difference_of_squares.c
blob: 8a422eb6c7a8cd0f467c1f265b9bc54f5bca81ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "difference_of_squares.h"
#include <math.h>

/*
   Returns the sum of squares upto nth Natural number.

   The difference in squares increases by a incrementing (by 2) step.

   Sequence:
   1 4 9 16 25 36 49 64 81 100

   4 - 1 = 3
   9 - 4 = 5    (3 + 2 = 5)
   16 - 9 = 7   (5 + 2 = 7)

   step is our incrementing step, we increment it by 2 in every
   iteration and add it to previous square to get the current term.
*/
unsigned int sum_of_squares(unsigned int number) {
    unsigned int sum = 1;
    unsigned int step = 1;
    unsigned int prev_sq = 1;
    for (; number > 1; number--) {
        step += 2;
        prev_sq += step;
        sum += prev_sq;
    }
    return sum;
}

unsigned int square_of_sum(unsigned int number) {
    unsigned int sum = 0;
    for (; number > 0; number--)
        sum += number;
    return pow(sum, 2);
}

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