Skip to content

Commit ee9727f

Browse files
authored
feat: perfect cube (TheAlgorithms#122)
* feat: perfect cube * fix: errors due to floating-point approximation
1 parent 4b953f3 commit ee9727f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

maths/perfect_cube.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* A number is a perfect cube, if the cube root is an integer.
3+
*
4+
* @param n The number to check.
5+
*/
6+
7+
export const perfectCube = (n: number): boolean => {
8+
return Math.round(n ** (1 / 3)) ** 3 === n;
9+
};

maths/test/perfect_cube.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { perfectCube } from "../perfect_cube";
2+
3+
describe('perfect cube tests', () => {
4+
it.each([
5+
[27, true],
6+
[9, false],
7+
[8, true],
8+
[12, false],
9+
[64, true],
10+
[151, false],
11+
[125, true],
12+
])('The return value of %i should be %s', (n, expectation) => {
13+
expect(perfectCube(n)).toBe(expectation);
14+
});
15+
});

0 commit comments

Comments
 (0)