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

Added FastPower #731

Merged
merged 2 commits into from May 10, 2019
Merged

Added FastPower #731

merged 2 commits into from May 10, 2019

Conversation

@DDullahan
Copy link
Contributor

@DDullahan DDullahan commented Apr 8, 2019

Added FastPower

We may calculate power with loops, but what if the index is too large ?

FastPower aims to calculate quickly in this circumstances with time complexity O(log k), where k is the index.

Added FastPower
Copy link

@havanagrawal havanagrawal left a comment

This looks like the same functionality as modPow, so the tests should use this as the "expected" value.

public class FastPowerTest {

@Test
public void test() {

This comment has been minimized.

@havanagrawal

havanagrawal May 2, 2019

This should have assert* statements, not printlns.

BigInteger ans = BigInteger.ONE;
while (!k.equals(BigInteger.ZERO)) {
int odd = k.mod(new BigInteger("2")).compareTo(BigInteger.ZERO);
if(odd == 1){

This comment has been minimized.

@havanagrawal

havanagrawal May 2, 2019

The result of compareTo should only be compared with 0, there is no guarantee that it will be 1, only that it may be negative, 0 or positive.

if(odd == 1){
ans = ans.multiply(n).mod(mod);
}
k = k.divide(new BigInteger("2"));

This comment has been minimized.

@havanagrawal

havanagrawal May 2, 2019

Instead of using new BigInteger("2") in a loop, we could declare it as a constant outside:

public static final BigInteger TWO = new BigInteger("2");
@DDullahan
Copy link
Contributor Author

@DDullahan DDullahan commented May 3, 2019

Thanks for your review @havanagrawal , I have fixed these problems.

@yanglbme yanglbme merged commit 64bd1a1 into TheAlgorithms:Development May 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.