0
$\begingroup$

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!
$\endgroup$
  • $\begingroup$ There are $N^3$ little cubes. if you remove all the ones with paint on them, you get $(N-2)^3$ little cubes. $\endgroup$ – lulu Jul 29 '16 at 10:39
  • $\begingroup$ Do you $N$ times horizontally, then $N$ times vertically, then $N$ times "the other" vertically, giving $(N+1)^3$ little cubes in total? Or do you do something else (in which case I wouldn't call the small resulting pieces "cubes")? $\endgroup$ – Arthur Jul 29 '16 at 10:42
  • $\begingroup$ I updated the description to be more detailed, I hope it is better now ! Added expected results! $\endgroup$ – Flame_Phoenix Jul 29 '16 at 10:45
  • $\begingroup$ Sorry, I assumed we were cutting into $N$ cubes in each direction (hence $N-1$ cuts). That is, your $N$ corresponds to my $N+1$. The answer to the problem the way you phrased it is $(N+1)^3-(N-1)^3$. $\endgroup$ – lulu Jul 29 '16 at 10:51
2
$\begingroup$

(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.

$\endgroup$
  • $\begingroup$ Wow, I would not have done that ! Great idea, thanks ! $\endgroup$ – Flame_Phoenix Jul 29 '16 at 11:04
  • $\begingroup$ I would like to add, that if N = 0 the result is 1 :D $\endgroup$ – Flame_Phoenix Jul 29 '16 at 11:44
  • $\begingroup$ I shall update this question soon with the answer ! $\endgroup$ – Flame_Phoenix Jul 29 '16 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.