A Rubik cube usually have 6 sides. Take 3×3 rubik cube for example, each side has 9 little squares (1×1), 4 medium squares (2×2) and 1 big square (3×3). Therefore, there are 6 x (9 + 4 + 1) = 84 squares on a 3×3 rubik cube.
How about counting the rectangles? It is a bit hard to count. Let’s write a function to count it. The algorithm is to bruteforce a pair of points (from top left corner to the right bottom). Then we compute the two sides of the rectangle to see if it is a square.
function countSquaresAndRectangles(N) {
let squares = 0;
let rectangles = 0;
for (let a = 0; a <= N; a ++) {
for (let b = 0; b <= N; b ++) {
for (let c = a + 1; c <= N; c ++) {
for (let d = b + 1; d <= N; d ++) {
if (d - b === c - a) {
squares ++;
} else {
rectangles ++;
}
}
}
}
}
return [6 * squares, 6 * rectangles];
}
let ans = countSquaresAndRectangles(3);
console.log("There are " + ans[0] + " squares, and " + ans[1] + " rectangles.");
There are 84 squares, and 132 rectangles in a 3×3 rubik cube. For a 7×7 rubik cube, it will be hard to count. But the computers are really good at it (in fact, the above Javascript code runs pretty fast, for a small N).
There are 840 squares, and 3864 rectangles for a 7×7 rubik cube.
See also: Teaching Kids Programming – Counting the Number of Squares and Rectangles of a NxN Rubik Cube
–EOF (The Ultimate Computing & Technology Blog) —
367 wordsLast Post: Find the 10001st Prime Number
Next Post: How to Use Priority Queue in Java or C++ to Compute Last Stone Weight?

