The link for the problem "Feynman" is :

http://www.spoj.com/problems/SAMER08F/

The solution is very simple to consider the following example:

let we consider the different dimension squares:

Total Number of Square formed

1 * 1 square grid has - 1

2* 2 square grid has - 5

3 * 3 square grid has - 14

...

..

..

From this, we can deduce a formula for n*n square grid has

=> (n*(n+1)*(2*n+1))/6; number of squares.

The complete CPP solution for the problem "Feynman":

http://www.spoj.com/problems/SAMER08F/

The solution is very simple to consider the following example:

Total Number of Square formed

1 * 1 square grid has - 1

2* 2 square grid has - 5

3 * 3 square grid has - 14

...

..

..

From this, we can deduce a formula for n*n square grid has

=> (n*(n+1)*(2*n+1))/6; number of squares.

The complete CPP solution for the problem "Feynman":

```
#include <iostream>
using namespace std;
int main() {
long long int i,j,n,ans;
while(1)
{
cin>>n;
if(n==0)
break;
ans=(n*(n+1)*(2*n+1))/6;
cout<<ans<<endl;
}
return 0;
}
```

## No comments:

## Post a comment