DEV Community

Peter Kim Frank
Peter Kim Frank Subscriber

Posted on

Project Euler #6 - Sum Square Difference

Continuing the wonderful community solutions to Project Euler.

This is Problem 6, sum square difference.

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

1² + 2² + ... + 10² = 385

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

(1 + 2 + ... + 10)² = 55² = 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.

Latest comments (31)

Collapse
 
jacklangcreator profile image
Sagnik Ray

public class ProjectEuler{
static int n = 100;
public static void main(String []args){
System.out.println(differencehelper());

}
static int sumsquare(int n){
int sum = 0 ;
sum = (n*(n+1)(2*n+1))/6;
return sum;
}
static int squaresum(int n){
int sum = 0;
sum = (n
(n+1))/2;
return (sum *sum);
}
static int differencehelper(){
return squaresum(n) - sumsquare(n);
}



}

Easy Java Solution using Gauss' Formula Hope you liked it ;)

Collapse
 
crownedprinz profile image
Ademola John • Edited

Here is my Javascript Solution


function sumSquareDifference(n) {
  // Good luck!
  let a = 0,b=0,c=0;
  for(let i = 1;i<=n;i++){
    a+= (Math.pow(i,2));
    b+=i
  }
c=Math.pow(b,2)-a

console.log(a);
console.log(b);
  return c;
}
Collapse
 
rahyhub profile image
rahy-hub

my code with C++

/*Sum square difference

Problem 6
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.*/

include

using namespace std;
int square_sum(int s);

int main()
{
int limit=100 ,sum=0 ,sumQ=0;
for(int i=1;i<=limit;i++)
{
sumQ=sumQ+(i*i);
sum =sum+i;
}

cout<<"the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum is \n ";
cout<< square_sum(sum)-sumQ ;  // without function : cout<<sum*sum-sumQ
return 0;

}

int square_sum(int s)
{
return s*s;
}
Output >> 25164150

Collapse
 
nurchikgit profile image
nurchikgit

not bad)))

Collapse
 
seyedmahmoudbayazid profile image
Mahmoud Bayazid

new_list = []

for i in range (1,101):
new_list.append(i*2)
a = sum(new_list)
b = sum(range(1,101))
*2
print(b-a)

Collapse
 
seyedmahmoudbayazid profile image
Mahmoud Bayazid • Edited

new_list = []

for i in range (1,101):
new_list.append(i^2)
a = sum(new_list)
b = sum(range(1,101))^2
print(b-a)

Collapse
 
hishamkhr profile image
hishamkhr

hi
i try this:
sum of numbers= n(n+1)/2
then i get the square.
after that i found an equation about sum of squares:
(2*n^3 +3*n^2+n )/6
but i didn't get right result !!!!
so where i lost the control ?
thanks

Collapse
 
olivierpicault profile image
Olivier Picault

From ProjectEuler:

Please do not deprive others of going through the same process by publishing your solution outside of Project Euler; for example, on other websites, forums, blogs, or any public repositories (e.g. GitHub), et cetera. Members found to be spoiling problems will have their accounts locked.

:)

Collapse
 
brightone profile image
Oleksii Filonenko

Elixir:

(1..100 |> Enum.map(&:math.pow(&1, 2)) |> Enum.sum()) -
  (1..100 |> Enum.sum() |> :math.pow(2))
Collapse
 
comaldave profile image
David Skinner

Go Gopher

goplay.space/#rQ0XekiCknZ

// func SumSquareDifference receives the max range
// and returns the difference of the sumSquare and the squareSum.
func SumSquareDifference(x int) int {
    sum := 0
    sumSquare := 0

    for i := 1; i <= x; i++ {
        sum += i
        sumSquare += i * i
    }

    squareSum := sum * sum

    return squareSum - sumSquare
}

You can see the complete program on the playground link.

  • I have included a block that confirms that my function provides the expected response.
  • I then start a timer so I can print the elapsed time to run the function.
  • After printing the results, I include a comment defining the Output, If the output changes it will generate an error on my computer.
  • The language is Go but it is more a C++ style, I am not the best Go programmer.
  • Code needs to be readable, compilers can do the optimization.
Collapse
 
fossilizeddev profile image
Carlos E. Barboza • Edited

Swift:

func sumSquareDifference(range: Int) -> Int {
    var sumOfSquares = 0
    var squareOfSum = 0
    for number in 1..<range+1 { //Is there a better way? Other than range+1?
        sumOfSquares += (number*number)
        squareOfSum += number
    }
    squareOfSum = squareOfSum*squareOfSum
    return (squareOfSum - sumOfSquares)
}

print(sumSquareDifference(range:100))