Cho kích thước bàn cờ n x n. Tính tổng số ô vuông (các kích thước 1x1, 2x2, ..., nxn) trong bàn cờ.
Ví dụ
n = 4: 1x1=16, 2x2=9, 3x3=4, 4x4=1 → Tổng = 30
Công thức chung
Tổng = 1² + 2² + 3² + ... + n² = n(n+1)(2n+1)/6
Ví dụ minh họa (C++)
#include <iostream>
using namespace std;
int calcSquaresCount(int n) {
int squareCount = (n * (n + 1) * (2 * n + 1)) / 6;
return squareCount;
}
int main() {
int n = 6;
cout << "The total number of squares of size " << n << "X" << n << " is " << calcSquaresCount(n);
return 0;
}
Đầu ra
The total number of squares of size 6X6 is 91