Sum Square Difference

Problem 6 Project Euler 

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

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

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.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
    static void Main(String[] args) {
        int t = Convert.ToInt32(Console.ReadLine());
        for(int a0 = 0; a0 < t; a0++){
            long n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(Difference(n));
        }
    }
    
    static long Difference(long n)
    {
        long i= (n*n*n*n +2*n*n*n +n*n)/4;
        long j= (2*n*n*n + 3*n*n +n)/6;
        
        return i-j;
    }
}
Live Code on Ide One

Comments

Popular Posts