-
Notifications
You must be signed in to change notification settings - Fork 19.9k
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
base: master
Are you sure you want to change the base?
fix: handle zero case in Armstrong number check to avoid Math.log10(0… #6229
Conversation
Codecov ReportAttention: Patch coverage is
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. 🚀 New features to boost your workflow:
|
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; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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
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.