Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions project_euler/problem_6/o(1)sol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Project Euler Problem 6
// Find the difference between the sum of the squares of the first n natural
// numbers and the square of the sum of the first n natural numbers.
//
// Formula used:
// Sum of squares = n(n+1)(2n+1)/6
// Square of sum = (n(n+1)/2)^2
//
// Example:
// Input : 100
// Output: 25164150

#include <stdio.h>

// Function to compute sum of squares using formula
long long sum_of_square(int n)
{
return (long long)n * (n + 1) * (2 * n + 1) / 6;
}

// Function to compute square of sum using formula
long long square_of_sum(int n)
{
long long sum = (long long)n * (n + 1) / 2;
return sum * sum;
}

int main()
{
int n;
scanf("%d", &n);

long long answer = square_of_sum(n) - sum_of_square(n);
printf("%lld\n", answer);

return 0;
}
//this calculates in o(1) ,enter the n till which u want to find. for our case (in question) its 100