Skip to content

Latest commit

 

History

History
28 lines (26 loc) · 608 Bytes

2018-02-28-SPOJ-3410.md

File metadata and controls

28 lines (26 loc) · 608 Bytes
title comments
SPOJ 3410: SAMER08F - Feynman
true

Explanation

How many squares a (4x4) square holds? the answer is-

16 (1x1) squares + 4 (2x2) squares, 9 (3x3) squares, 1 (4x4) squares

Meaning, the answer is $$n^2+(n-1)^2+(n-2)^2+...+1^2$$ Which is equivalent to,
$$ \sum_{i=1}^{n} i^2 = 1^2 + 2^2 + 3^2 +...+ n^2 = \frac{n(n+1)(2n+1)}{6} $$

Solution

#include<stdio.h>
int main()
{
    int n,x;
    while(scanf("%d",&n)&&n!=0)
    {
        x=(n*(n+1)*((2*n)+1))/6;
        printf("%d\n",x);
    }
    return 0;
}