Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added FastPower#731

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
yanglbme merged 2 commits intoTheAlgorithms:DevelopmentfromDDullahan:Development
May 10, 2019
Merged

Added FastPower#731

yanglbme merged 2 commits intoTheAlgorithms:DevelopmentfromDDullahan:Development
May 10, 2019

Conversation

@DDullahan
Copy link
Contributor

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.

DDullahan reacted with laugh emoji
Added FastPower
Copy link

@havanagrawalhavanagrawal left a comment

Choose a reason for hiding this comment

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

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

public class FastPowerTest {

@Test
public void test() {

Choose a reason for hiding this comment

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

This should haveassert* statements, notprintlns.

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

Choose a reason for hiding this comment

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

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

DDullahan reacted with thumbs up emoji
if(odd == 1){
ans = ans.multiply(n).mod(mod);
}
k = k.divide(new BigInteger("2"));

Choose a reason for hiding this comment

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

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

publicstaticfinalBigIntegerTWO =newBigInteger("2");

@DDullahan
Copy link
ContributorAuthor

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

@yanglbmeyanglbme merged commit64bd1a1 intoTheAlgorithms:DevelopmentMay 10, 2019
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@yanglbmeyanglbmeyanglbme approved these changes

+1 more reviewer

@havanagrawalhavanagrawalhavanagrawal left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@DDullahan@havanagrawal@yanglbme

[8]ページ先頭

©2009-2025 Movatter.jp