Skip to content

NeonNumber algorithm in maths package#7420

Open
AshishKumar1297 wants to merge 8 commits into
TheAlgorithms:masterfrom
AshishKumar1297:ashish-contribution
Open

NeonNumber algorithm in maths package#7420
AshishKumar1297 wants to merge 8 commits into
TheAlgorithms:masterfrom
AshishKumar1297:ashish-contribution

Conversation

@AshishKumar1297
Copy link
Copy Markdown

Added NeonNumber algorithm that checks if a
number is a Neon Number.
Example: 9 → 9²=81 → 8+1=9 ✓

  • I have read CONTRIBUTING.md
  • This pull request is my own work
  • All filenames are in PascalCase
  • All functions and variable names follow Java naming conventions
  • All new algorithms have a URL in their comments
  • All new algorithms include a corresponding test class

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.54%. Comparing base (e814d97) to head (5691cb7).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7420   +/-   ##
=========================================
  Coverage     79.53%   79.54%           
- Complexity     7177     7180    +3     
=========================================
  Files           798      799    +1     
  Lines         23474    23481    +7     
  Branches       4617     4619    +2     
=========================================
+ Hits          18671    18678    +7     
  Misses         4055     4055           
  Partials        748      748           

☔ 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.

Copy link
Copy Markdown
Contributor

@prashantpiyush1111 prashantpiyush1111 left a comment

Choose a reason for hiding this comment

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

Overall structure is good — final class, private constructor,
and JavaDoc are all in place.

However, there are a few issues that need to be fixed
before merging:

  1. Integer overflow in isNeon() method
  2. No handling for negative inputs
  3. isNeon(0) works by coincidence, not by logic
  4. Missing edge case tests

Please address the inline comments.

* @return true if neon number, false otherwise
*/
public static boolean isNeon(final int number) {
int square = number * number;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Integer overflow risk! For large inputs (e.g. number = 50000),
number * number exceeds Integer.MAX_VALUE and gives wrong result.

Fix:
long square = (long) number * number;

*/
public static boolean isNeon(final int number) {
int square = number * number;
int digitSum = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No validation for negative numbers. isNeon(-5) won't crash
but behavior is unintended.

Fix: Add at the start of method:
if (number < 0) {
throw new IllegalArgumentException("Input must be non-negative");
}

int square = number * number;
int digitSum = 0;
int temp = square;
while (temp > 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When number = 0, temp = 0, so this loop never runs.
isNeon(0) returns true only by coincidence.

Fix:
if (number == 0) return true;

public class NeonNumberTest {

@Test
public void testIsNeonTrue() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

assertTrue(NeonNumber.isNeon(0)) passes but for wrong reason —
loop never executes for 0. Consider adding explicit test
that verifies the logic, not just the output.

}

@Test
public void testIsNeonFalse() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing edge case tests:

  1. Negative input:
    assertThrows(IllegalArgumentException.class,
    () -> NeonNumber.isNeon(-1));

  2. Large number to catch overflow:
    assertFalse(NeonNumber.isNeon(50000));

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