From bbc549c3eab6e2b96099f1ddefce65afffd2d40a Mon Sep 17 00:00:00 2001 From: Loosetooth Date: Sat, 1 Feb 2025 23:26:21 +0100 Subject: [PATCH] add kata --- 6kyu/build-a-pile-of-cubes/description.md | 63 +++++++++++++++++++++++ 6kyu/build-a-pile-of-cubes/solution.ts | 10 ++++ 6kyu/build-a-pile-of-cubes/tests.ts | 11 ++++ 3 files changed, 84 insertions(+) create mode 100644 6kyu/build-a-pile-of-cubes/description.md create mode 100644 6kyu/build-a-pile-of-cubes/solution.ts create mode 100644 6kyu/build-a-pile-of-cubes/tests.ts diff --git a/6kyu/build-a-pile-of-cubes/description.md b/6kyu/build-a-pile-of-cubes/description.md new file mode 100644 index 0000000..42dffa6 --- /dev/null +++ b/6kyu/build-a-pile-of-cubes/description.md @@ -0,0 +1,63 @@ +Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of +n +3 +n +3 + , the cube above will have volume of +( +n +− +1 +) +3 +(n−1) +3 + and so on until the top which will have a volume of +1 +3 +1 +3 + . + +You are given the total volume m of the building. Being given m can you find the number n of cubes you will have to build? + +The parameter of the function findNb (find_nb, find-nb, findNb, ...) will be an integer m and you have to return the integer n such as +n +3 ++ +( +n +− +1 +) +3 ++ +( +n +− +2 +) +3 ++ +. +. +. ++ +1 +3 += +m +n +3 + +(n−1) +3 + +(n−2) +3 + +...+1 +3 + =m if such a n exists or -1 if there is no such n. + +Examples: +findNb(1071225) --> 45 + +findNb(91716553919377) --> -1 \ No newline at end of file diff --git a/6kyu/build-a-pile-of-cubes/solution.ts b/6kyu/build-a-pile-of-cubes/solution.ts new file mode 100644 index 0000000..3d82e6f --- /dev/null +++ b/6kyu/build-a-pile-of-cubes/solution.ts @@ -0,0 +1,10 @@ +export function findNb(m: number): number { + // start with 1 and just keep adding cubes until we reach larger or equal to m + let sum = 0; + let i = 1; + while (sum < m) { + sum += i ** 3; + i++; + } + return sum === m ? i - 1 : -1; +} \ No newline at end of file diff --git a/6kyu/build-a-pile-of-cubes/tests.ts b/6kyu/build-a-pile-of-cubes/tests.ts new file mode 100644 index 0000000..86e70f8 --- /dev/null +++ b/6kyu/build-a-pile-of-cubes/tests.ts @@ -0,0 +1,11 @@ +import { findNb } from './solution'; +import { assert } from "chai"; + +describe("Fixed Tests nbMonths", function() { + it("Basic tests", function() { + assert.strictEqual(findNb(4183059834009), 2022); + assert.strictEqual(findNb(24723578342962), -1); + assert.strictEqual(findNb(135440716410000), 4824); + assert.strictEqual(findNb(40539911473216), 3568); + }); +}); \ No newline at end of file