You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While learning about Fast Powering Algorithm, I notice that a minor tweak can be made to the fastPowering.js file.
In both of the cases whether the power is odd or event, calculated multiplier will be the same. So, we can calculate multiplier before the if clause and return the multiplication based on the nature of power.
exportdefaultfunctionfastPowering(base,power){if(power===0){// Anything that is raised to the power of zero is 1.return1;}// multiplier will be same whether power is even or oddconstmultiplier=fastPowering(base,Math.floor(power/2));if(power%2===0){// If the power is even...// we may recursively redefine the result via twice smaller powers:// x^8 = x^4 * x^4.returnmultiplier*multiplier;}// If the power is odd...// we may recursively redefine the result via twice smaller powers:// x^9 = x^4 * x^4 * x.returnmultiplier*multiplier*base;}
@trekhleb Let me know your thoughts. And if you're okay with this implementation I can raise a PR.
The text was updated successfully, but these errors were encountered:
While learning about Fast Powering Algorithm, I notice that a minor tweak can be made to the
fastPowering.js
file.In both of the cases whether the
power
is odd or event, calculatedmultiplier
will be the same. So, we can calculatemultiplier
before the if clause and return the multiplication based on the nature ofpower
.@trekhleb Let me know your thoughts. And if you're okay with this implementation I can raise a PR.
The text was updated successfully, but these errors were encountered: