javascript-algorithms icon indicating copy to clipboard operation
javascript-algorithms copied to clipboard

added the condition to handle negative integers for fastPowering algorithm.

Open nikikalwar opened this issue 4 years ago • 1 comments

Issue:

Error:RangeError: Maximum call stack size exceeded #590

I have edited the fastpowering algorithm to handle negative integers and the test file to check the negative integers condition.

nikikalwar avatar Dec 15 '20 01:12 nikikalwar

How about checking for power equals 1, this can avoid from calculate fastPowering(base, 1) = fastPowering(base, 0) * fastPowering(base, 0) * base everytimes.

if (power === 0) {
  // Anything that is raised to the power of zero is 1.
  return 1;
}

if (power === 1) {
  return base;
}

.
.
.

JackyYin avatar Dec 17 '20 10:12 JackyYin