7 kyu

Count cubes in a Menger Sponge

702 of 1,667rasmus_ri

Description:

The Menger Sponge is a three-dimensional fractal, first described by Karl Menger in 1926.

Mengers Sponge (Level 0-3)

An illustration of the iterative construction of a Menger sponge

A method of constructing a Menger Sponge can be visualized as follows:

  1. Start from a cube (first part of image).
  2. Scale down the cube so that side length is 1/3 of its original, and make 20 copies of it.
  3. Place the copies so that they measure the same size as the original cube but without its central parts (next part of image)
  4. Repeat the process from step 2 for the new smaller cubes from the previous step.
  5. In each iteration (e.g. repeating the last three steps), the effect will be that parts of the cube will be removed, they'll never be added. Menger sponge will always consist of parts will never be removed, regardless of how many iterations you do.

An alternative explanation:

  1. Start from a cube (first part of image).
  2. Devide each cube into 27 equal sized cubes.
  3. Remove the middle-cube and the six cubes on each side of the group of 27 cubes (second part of image).
  4. Repeat the process from step 2 for the smaller cubes (third and fourth part of image).

Task

In this kata you will create a function that takes non negative integers (from 0 to n) and return the amount of cubes that the Menger Sponge would have in that specific iteration.

Example

For n = 0, the ouptut should be 1;
For n = 1, the output should be 20;
For n = 2, the output should be 400;
For n = 3, the output should be 8000;
for n = 4, the output should be 160000;
For n = 5, the output should be 3200000;
For n = 6, the output should be 64000000.

Happy coding!

Algorithms
Mathematics

Stats:

CreatedOct 2, 2017
PublishedOct 2, 2017
Warriors Trained2319
Total Skips31
Total Code Submissions2696
Total Times Completed1667
Python Completions702
JavaScript Completions877
Ruby Completions53
Dart Completions34
C Completions42
C++ Completions86
Total Stars23
% of votes with a positive feedback rating89% of 428
Total "Very Satisfied" Votes348
Total "Somewhat Satisfied" Votes65
Total "Not Satisfied" Votes15
Total Rank Assessments15
Average Assessed Rank
7 kyu
Highest Assessed Rank
7 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • rasmus_ri Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • rowcased Avatar
  • hakatom Avatar
  • hobovsky Avatar
  • ejini战神 Avatar
  • Just4FunCoder Avatar
  • yLaWy Avatar
Ad