Repository navigation
#044 Stack the Boxes
Write a function that takes a number n and returns the number of stacked boxes in a model n levels high, visible and invisible.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/044 Stack the Boxes.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//Write a function that takes a number n and returns the number of stacked boxes in a model n levels high, visible and invisible.
function stackBoxes(n) {
return n * n;
}
import * as Test from '../testHelper.js';
Test.assertEquals(stackBoxes(1), 1)
Test.assertEquals(stackBoxes(2), 4)
Test.assertEquals(stackBoxes(0), 0)
Test.assertEquals(stackBoxes(5), 25)
Test.assertEquals(stackBoxes(27), 729)
Test.assertEquals(stackBoxes(196), 38416)
Test.assertEquals(stackBoxes(512), 262144)Cross-language explanation preserved from the study materials
Reference file: #037 Stack the Boxes.txt. These older notes use a separate numbering scheme and may describe C# code, external diagrams or benchmark timings. They are reference material, not measured JavaScript performance. The current implementation and assertions above are authoritative.
# [Stack the Boxes](https://edabit.com/challenge/h2BGWkeBcoiW2tSXX)
Published by [Nomore](https://edabit.com/user/p6ESyNjSyY6QbwMDW)
algebra algorithms logic math numbers
Some of the cubes are hidden behind other cubes. Model one consists of one cube. Model two consists of four cubes, and so on...
Write a function that takes a number `n` and returns the number of stacked boxes in a model `n` levels high, visible and invisible.
`StackBoxes(1) ➞ 1`
`StackBoxes(2) ➞ 4`
`StackBoxes(0) ➞ 0`
Step `n` is a positive integer.
### Time Complexity:
The time complexity of the `StackBoxes` function is _O(1)_.
The function performs a single multiplication operation, regardless of the input size.
### Space Complexity:
The space complexity is also _O(1)_. The function only needs storage for the input parameter 𝑛 and the return value (an integer).

//Here's an image of four models. Some of the cubes are hidden behind other cubes.
//Model one consists of one cube. Model two consists of four cubes, and so on...
//Write a function that takes a number n and returns the number of stacked boxes in a model n levels high, visible and invisible.
using BenchmarkDotNet.Attributes;
using System;
namespace Challenges
{
public class Program37
{
public static int StackBoxes(int n) => n * n;
}
public class BenchmarkProgram37
{
[Benchmark]
[Arguments(1)]
[Arguments(2)]
[Arguments(0)]
[Arguments(5)]
[Arguments(27)]
[Arguments(196)]
[Arguments(512)]
public int StackBoxes(int n) => Program37.StackBoxes(n);
}
}
using System;
using Challenges.Edabit;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class Tests37
{
[Test]
[TestCase(1, 1)]
[TestCase(2, 4)]
[TestCase(0, 0)]
[TestCase(5, 25)]
[TestCase(27, 729)]
[TestCase(196, 38416)]
[TestCase(512, 262144)]
public void FixedTest(int a, int expectedResult)
{
int result = Program37.StackBoxes(a);
Assert.That(result, Is.EqualTo(expectedResult));
}
}
}