Compare commits

...

17 Commits

Author SHA1 Message Date
nikikalwar
5aa20e0b3b
Merge 310343e0e1 into 2c67b48c21 2024-04-25 08:25:40 +08:00
nikikalwar
310343e0e1
Update fastPowering.test.js 2021-06-23 22:50:12 +05:30
nikikalwar
2c221989b1
Update fastPowering.js 2021-06-23 22:49:33 +05:30
Niki.kalwar
604e9cdd4e edited the comments 2021-06-23 22:18:33 +05:30
nikikalwar
43b806f89c
Update fastPowering.test.js 2021-06-18 14:52:15 +05:30
nikikalwar
f7bb28a0fd
Merge branch 'trekhleb:master' into my-test 2021-06-18 14:50:34 +05:30
nikikalwar
b3da0ab5ef
Merge branch 'master' into my-test 2021-02-06 18:54:51 +05:30
nikikalwar
5daae15052
Merge branch 'master' into my-test 2020-12-19 03:32:16 +05:30
nikikalwar
5ed34c7d05 fixed the boundary condition and linted 2020-12-17 06:55:48 +05:30
nikikalwar
506aefbc78 fixed the boundary condition 2020-12-17 06:50:04 +05:30
nikikalwar
3b1fcd8333 fixed:error 2020-12-17 06:39:36 +05:30
nikikalwar
a64c587080 Merge branch 'my-test' of https://github.com/nikikalwar/javascript-algorithms into my-test 2020-12-16 22:59:21 +05:30
nikikalwar
0cb77d0454
Merge branch 'master' into my-test 2020-12-16 22:37:05 +05:30
nikikalwar
c82ceabee8 Merge branch 'master' into my-test 2020-12-16 19:41:42 +05:30
nikikalwar
e076e12dee
Merge branch 'master' into my-test 2020-12-16 19:24:42 +05:30
Oleksii Trekhleb
aad101181c
Merge branch 'master' into my-test 2020-12-15 08:16:08 +01:00
nikikalwar
4eb570341d added the condition to handle negative integers 2020-12-15 07:12:46 +05:30
2 changed files with 9 additions and 0 deletions

View File

@ -19,5 +19,8 @@ describe('fastPowering', () => {
expect(fastPowering(16, 16)).toBe(18446744073709552000);
expect(fastPowering(7, 21)).toBe(558545864083284000);
expect(fastPowering(100, 9)).toBe(1000000000000000000);
expect(fastPowering(5, -5)).toBe(0.0003200000000000002);
expect(fastPowering(-5, 5)).toBe(-3125);
expect(fastPowering(-5, -5)).toBe(-0.0003200000000000002);
});
});

View File

@ -14,6 +14,12 @@ export default function fastPowering(base, power) {
return 1;
}
if (power < 0) {
const powerNext = power * -1;
const baseNext = 1 / base;
return fastPowering(baseNext, powerNext);
}
if (power % 2 === 0) {
// If the power is even...
// we may recursively redefine the result via twice smaller powers: