QM6A editorial

PROBLEM LINK:

Practice

Contest

Author: Chandan Boruah

Tester: Chandan Boruah

Editorialist: Chandan Boruah

DIFFICULTY:

EASY

PREREQUISITES:

Maths/Iteration

PROBLEM:

Given a number n find the square of all the numbers in the sequence 1,2,3…n,n-1,n-2,…1

QUICK EXPLANATION:

Use formula or just iterate over the sequence.

EXPLANATION:

Form the sequence, iterate through all the numbers and sum the squares. Or use the formula for square of first n natural numbers n*(n+1)(2n+1)/6.

Solution in CS:

using System;
class some
{
	public static void Main()
	{
		int t=int.Parse(Console.ReadLine());
		for(int i=0;i<t;i++)
		{
			long l=long.Parse(Console.ReadLine());
			long ret=l*(l+1)*(2*l+1)/6;
			l--;
			ret+=l*(l+1)*(2*l+1)/6;
			Console.WriteLine(ret);
		}
	}
}