-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifference_of_sqs.c
More file actions
50 lines (37 loc) · 1.42 KB
/
difference_of_sqs.c
File metadata and controls
50 lines (37 loc) · 1.42 KB
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
/**
* Project Euler Problem 6:
*
* Find the difference between the sum of the squares of the first N natural numbers and
* square of the sum of the N first natural numbers.
*
* Explanation:
*
* 1. Sum of the squares of the first N natural numbers can be found out mathematically as:
*
* (N( N+1 )( 2N+1 )) / 6
* 2. Square of the sum of first N natural numbers can be found out mathematically as:
*
* ((N(N+1)) / 2)^2
* 3. So the difference would be (square of the sum - sum of the squares)
*
* 4. Instead of iterating through numbers and calculating each term, we directly use
* the formulas mentioned above to compute the result in O(1) time.
*/
#include <stdio.h>
unsigned long long sumSquareDiff(int N) {
unsigned long long sum_of_squares = (N * (N+1) * (2 * N + 1)) / 6;
unsigned long long sum = (N * (N+1)) / 2;
unsigned long long square_of_sum = sum * sum;
return (square_of_sum - sum_of_squares);
}
int main() {
int N;
printf("Enter the value of N: ");
if (scanf("%d", &N) != 1) {
printf("ERROR: Invalid input value of N. Expected one integer value. \n");
return -1; // A non-zero value indicating an error
}
unsigned long long result = sumSquareDiff(N);
printf("The difference between the square of the sum and the sum of the squares for the first %d natural numbers is: %llu\n", N, result);
return 0;
}