Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fastPowering): check when number is raided to power of one #1055

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jaw0r3k
Copy link

@jaw0r3k jaw0r3k commented Jul 30, 2023

The function didnt check when power is one so unnecessarily was going though multiplying base by ones

Copy link

@AmanKumarVerma11 AmanKumarVerma11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for power === 1 is redundant and can be removed.
The key insight is that when power reduces to 0 in the recursion, the base case of return 1 will be hit anyway. So checking explicitly for power === 1 is not needed.
By removing the check for power === 1, the code is cleaner and more minimal. The recursion and base case of power === 0 handles the case of power reducing to 1 implicitly.

Copy link

@chetannada chetannada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution by @jaw0r3k is right it's fastest then current solution.

Check the Screen shot for the proof
fastPowering (version 2) - JavaScript benchmark at JSBench me

Check Live Solution - Click Here

@lazarljubenovic
Copy link

lazarljubenovic commented Aug 21, 2023

This type of benchmark is usually not a good measurement for practical usage. More importantly, this type of "optimization" is not the goal of this repository -- it's about understanding data structures and algorithms, not squeezing out every nanosecond from the engine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants