Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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 is 83.33333% with 2 lines in your changes missing coverage. Please review.

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

Files with missing lines Patch % Lines
...c/main/java/com/thealgorithms/maths/Armstrong.java 83.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 Armstrong number
}

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

@DenizAltunkapan DenizAltunkapan Apr 29, 2025

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

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.

3 participants