How Many Squares/Rectangles Does a Rubik Cube Have?
- 时间:2020-09-10 12:55:33
- 分类:网络文摘
- 阅读:143 次
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.

rubik-cube-3×3
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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."); |
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).

rubik-cube-7×7
There are 840 squares, and 3864 rectangles for a 7×7 rubik cube.
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:土豆是一种非常普通的蔬菜,但其营养保健价值令人难以置信 大家别忘了喝碗营养丰富的腊八粥,它对女性朋友的好处尤其多 经常吃一点柚子好处多,柚子皮的作用也不少,以后别再浪费啦 牛奶是常见的营养饮品,如果选择不对,既浪费钱还影响健康 香蕉对身体健康有很多好处,教你用香蕉做一道美味粥吧 香菇与洋葱搭配在一起营养全面,使得保健功效会更好 分数的运算古代的分数除法 巧用份数解决问题 有趣的迷路问题 华罗庚的回归
- 评论列表
-
- 添加评论