# Project Euler Problem# 6 solution in C++

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

^{2}+ 2

^{2}+ … + 10

^{2}= 385

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

^{2}= 55

^{2}= 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.

Efficient solution without using any loops. So it will be constant time method:

#include<iostream>

using namespace std;

int main ()

{

int maxNum = 0;

int sum_of_square = 0;

int square_of_sum = 0;

cout << “Enter Maximum number: “;

cin >> maxNum;

sum_of_square = ((maxNum*(maxNum +1))*((2*maxNum)+1))/6;

square_of_sum *= square_of_sum = maxNum* (maxNum+1)/2;

cout << “sum of squares till ” << maxNum << ” is: ” << sum_of_square << endl;

cout << “squares of sum till ” << maxNum << ” is: ” << square_of_sum << endl;

cout << “difference between the sum of the squares the square of the sum: ” << square_of_sum – sum_of_square << endl;

}

###### Related articles

- Project Euler Problem# 5 solution in C++ (alikhuram.wordpress.com)
- Project Euler Problem #2 Solution in Java (alikhuram.wordpress.com)
- Project Euler: Problem #2 solution in C++ (alikhuram.wordpress.com)
- Project Euler: Problem # 3 solution in C++ (alikhuram.wordpress.com)
- Project Euler Problem #4 C++ solution: (alikhuram.wordpress.com)
- Project Euler Problem #1 Solution in Java (alikhuram.wordpress.com)
- Project Euler: Problem #1 solution in C++ (alikhuram.wordpress.com)

Posted on April 1, 2013, in Algorithms, C++, Project Euler and tagged constant time method, Project Euler, Project Euler solution in C++. Bookmark the permalink. 2 Comments.

Pingback: Project Euler Problem# 8 solution in C++ | Khuram Ali

Pingback: Project Euler problem#9 solution in C++ | Khuram Ali