Solution to Project Euler 6


Problem 6 has the following description:

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.

The solution is straight forward:

#include <stdio.h>

int main(){
  int i,j,y,sum1,sum2;
  sum=0;

  for (i=1;i<101;i++)
    sum+=i*i;
  
  y=0;
  for (j=1;j<101;j++){
    y=y+j;
  }
  sum2=y*y;
  
  printf("Answer: %dn",sum2-sum);

  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *