[Math] Puzzle: find number of cubes with 1 red face

algorithmspuzzle

Objective

Find an algorithm to count the number of cubes with at least one red side.

Puzzle

I have a puzzle that goes by the following:

Imagine you have a cube. Someone picks up that cube, and drops it in a
bucket of red paint. The whole cube is now painted red.

Then that person cuts through the cube in all three dimensions N times.

N is the number of times that the cube is cut in each dimension.

The objective is, given the number of cuts done (N) to find out how
many small cubes have at least one red face.

Expected results

Following are two expected results from the problem (the only cases I have):

N = 2 –> 26

N = 4 –> 98

Problem

I am sure there must be some mathematical formula to achieve this, but I am completely unaware of it. I need an algorithm to get me started on this puzzle!

Aftermath Solution

As promised, here is the algorithm in JavaScript. Turns out it was pretty simple in the end !

A simple version would be the following:

function countCubes(cuts){
  if(cuts === 0)
    return 1;

  return (6 * cuts * cuts) + 2;
}

However, for those savvy of tech, I also have a shorter version with the latest ECMA6. I know this probably doesn't belong here, but a promise is a promise !

I hope you enjoy!

/**
 * Given a cube painted in red and sliced `N` times in the XYZ axis, this function (that should be named countCubes instead of countSquares) counts the number
 * of small cubes with at least one red face.
 *
 * This function avoids the use of `pow` on purpose. See references for more info. 
 *
 * Special thanks to the people in math.exchange, they were kind and awesome.
 *
 * @param    {number}    cuts    The number of 3D cuts done to the painted cube.
 * @returns  {number}    The number of cubes with at least one red face.
 * @see      {@link http://math.stackexchange.com/questions/1874787/puzzle-find-number-of-cubes-with-1-red-face} for the mathematical algorithm behind the scenes. 
 * @see      {@link https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions} for information about Arrow functions in ECMA6
 * @see      {@link http://stackoverflow.com/questions/18382967/is-math-pow-more-expensive-than-multiplication-using-temporary-assignments} Comparison of multiply VS `pow`
 */
const countSquares = cuts => cuts ? (6 * cuts * cuts) + 2 : 1;

PS:

  • Special kudos to all the kind people in math.exchange that helped. I was not expecting such a warm welcome for sure!

Best Answer

(Assuming $N\geq1$.) After $N$ cuts, there are $(N+1)^3$ small cubes in total. If you strip away the outer layer of cubes with paint on them, there are $(N-1)^3$ cubes left. That means that the number of cubes you stripped away is $$(N+1)^3-(N-1)^3=6N^2+2$$Alternative solution: There are a total of $6(N+1)^2$ painted small faces. There are eight small cubes that have three faces painted (the corners), so we must subtract a total of $16$ to account for that (they make up $24$ painted faces, buy only eight cubes with red on them). There are also $12(N-1)$ small cubes that have two faces each painted, so we need to subtract $12N-12$ for that. In total $$ 6(N+1)^2-12(N-1)-16=6N^2+2 $$ small cubes with paint on one or more faces.