add kata
This commit is contained in:
63
6kyu/build-a-pile-of-cubes/description.md
Normal file
63
6kyu/build-a-pile-of-cubes/description.md
Normal file
@@ -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
|
||||
10
6kyu/build-a-pile-of-cubes/solution.ts
Normal file
10
6kyu/build-a-pile-of-cubes/solution.ts
Normal file
@@ -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;
|
||||
}
|
||||
11
6kyu/build-a-pile-of-cubes/tests.ts
Normal file
11
6kyu/build-a-pile-of-cubes/tests.ts
Normal file
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user