Home > Web Front-end > JS Tutorial > Fun JavaScript question: Count the number of cubes filled with paint

Fun JavaScript question: Count the number of cubes filled with paint

黄舟
Release: 2017-02-15 14:49:40
Original
1827 people have browsed it

Suppose I have a cube of iron, and I soak it whole into a bucket of red paint.

Please note that the pigment cannot penetrate into the iron block, which means it can only dye the surface.

Then, I fished out the iron block and cut it.

The iron block has three dimensions as a cube. I made N flat cuts on each dimension to turn it into smaller cubes.

For example, if I cut each face 2 times, I can get 27 small cubes, 3*3*3.

Now the question is, after cutting N times, how many of the small cubes obtained are dipped in red paint? (Things that are dyed on one or more sides are counted)

The idea is very simple:

Require the number of cubes dipped in paint, subtract the undyed cubes from the total number of small cubes The number is available.

Note:

If I did not cut, the result should be 1.

So we have the following code:


	
	var countSquares = function(cuts){
		if(cuts == 0){return 1};
		return Math.pow((cuts+1),3) - Math.pow((cuts-1),3);
	}
Copy after login


The above is the fun JavaScript question: counting the number of cubes filled with paint, For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template