-
Notifications
You must be signed in to change notification settings - Fork 5
/
sum_square_difference.c
85 lines (72 loc) · 1.77 KB
/
sum_square_difference.c
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Sum square difference.
* https://projecteuler.net/problem=6
*/
#include <assert.h>
/**
* General approach to find sum of squares
* for sequence N.
*
* @param int limit
* @return
*/
unsigned long long sum_of_squares(int limit)
{
unsigned long long sum = 0;
for (int i = 1; i <= limit; i++) {
sum += (i * i);
}
return sum;
}
/**
* General approach to find square of sum
* for sequence N.
*
* @param int limit
* @return
*/
unsigned long long square_of_sum(int limit)
{
unsigned long long sum = 0;
for (int i = 1; i <= limit; i++) {
sum += i;
}
return sum * sum;
}
/**
* Sum of squares of N sequence is given by
* (N * (N+1) * (2N+1)) / 6.
*
* @param int limit
*
* @return unsigned long long
*/
unsigned long long sum_of_squares_efficient(int limit)
{
return (unsigned long long) (limit * (limit + 1) * (2 * limit + 1)) / 6;
}
/**
* Sum of digits of N sequence is given by
* (N * (N+1)) / 2.
*
* @param int limit
*
* @return unsigned long long
*/
unsigned long long square_of_sum_efficient(int limit)
{
int sum = (limit * (limit + 1)) / 2;
return (unsigned long long) sum * sum;
}
int main()
{
assert(2640 == (square_of_sum(10) - sum_of_squares(10)));
assert(41230 == (square_of_sum(20) - sum_of_squares(20)));
assert(1582700 == (square_of_sum(50) - sum_of_squares(50)));
assert(25164150 == (square_of_sum(100) - sum_of_squares(100)));
assert(2640 == (square_of_sum_efficient(10) - sum_of_squares_efficient(10)));
assert(41230 == (square_of_sum_efficient(20) - sum_of_squares_efficient(20)));
assert(1582700 == (square_of_sum_efficient(50) - sum_of_squares_efficient(50)));
assert(25164150 == (square_of_sum_efficient(100) - sum_of_squares_efficient(100)));
return 0;
}