Docs


Problem 6: Sum square difference

Problem

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Solution

/*
 * Sum square difference
 * Problem 6 <https://projecteuler.net/problem=6>
 *
 * Project Euler
 *
 * By Ankit R Gadiya
 */

#include <stdio.h>

int sum_of_square (int num);
int square_of_sum (int num);

int main (void)
{
	int limit = 100, difference;

	difference = square_of_sum (limit) - sum_of_square (limit);

	printf("Result: %i\n", difference);

	return 0;
}

int square_of_sum (int num)
{
	int sum = 0;
	for (int i = 1; i <= num; i++)
		sum += i;

	return (sum * sum);
}

int sum_of_square (int num)
{
	int sum = 0;
	for (int i = 1; i <= num; i++)
		sum += i * i;

	return sum;
}

prob6.c

Result

25164150