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

fix: handle zero case in Armstrong number check to avoid Math.log10(0…#6229

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

Open
Tejaswi1305 wants to merge1 commit intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromTejaswi1305:fix-armstrong-zero-case

Conversation

Tejaswi1305
Copy link

Adds a safe check for number == 0 in the Armstrong number logic to avoid a Math.log10(0) exception.
Also acknowledges 0 as a valid Armstrong number (since 0^1 = 0).

Why:
Without this, passing 0 causes an ArithmeticException due to log10(0). This PR ensures the code handles all valid inputs correctly.

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is83.33333% with2 lines in your changes missing coverage. Please review.

Project coverage is 73.80%. Comparing base(d866fbd) to head(c8b9d60).

Files with missing linesPatch %Lines
...c/main/java/com/thealgorithms/maths/Armstrong.java83.33%1 Missing and 1 partial⚠️
Additional details and impacted files
@@             Coverage Diff              @@##             master    #6229      +/-   ##============================================- Coverage     73.80%   73.80%   -0.01%  Complexity     5311     5311              ============================================  Files           673      673                Lines         18376    18378       +2       Branches       3553     3554       +1     ============================================+ Hits          13563    13564       +1  Misses         4265     4265- Partials        548      549       +1

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report?Share it here.

🚀 New features to boost your workflow:
  • ❄️Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +24 to 44
if (number < 0) {
return false; // Negative numbers cannot be Armstrong numbers
}
if (number == 0) {
return true; //0 is an Armstrongnumber
}

while (originalNumber > 0) {
long digit = originalNumber % 10;
sum += (long) Math.pow(digit, totalDigits); // The digit raised to the power of total number of digits and added to the sum.
originalNumber /= 10;
}
long sum = 0;
int totalDigits = (int) Math.log10(number) + 1;
long originalNumber = number;

return sum == number;
while (originalNumber > 0) {
long digit = originalNumber % 10;
sum += (long) Math.pow(digit, totalDigits);
originalNumber /= 10;
}

return sum == number;
}

}
Copy link
Collaborator

@DenizAltunkapanDenizAltunkapanApr 29, 2025
edited
Loading

Choose a reason for hiding this comment

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

Very thoughtful of you! Nice that you noticed. Please run Clang to format your code, as the pipeline is failing — the rest looks good to me! 😊@Tejaswi1305

@github-actionsGitHub Actions
Copy link

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@DenizAltunkapanDenizAltunkapanDenizAltunkapan requested changes

@yanglbmeyanglbmeAwaiting requested review from yanglbmeyanglbme is a code owner

@vil02vil02Awaiting requested review from vil02vil02 is a code owner

@BamaCharanChhandogiBamaCharanChhandogiAwaiting requested review from BamaCharanChhandogiBamaCharanChhandogi is a code owner

@alxkmalxkmAwaiting requested review from alxkmalxkm is a code owner

@siriaksiriakAwaiting requested review from siriaksiriak is a code owner

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@Tejaswi1305@codecov-commenter@DenizAltunkapan

[8]ページ先頭

©2009-2025 Movatter.jp