Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added ashish-contribution
Empty file.
32 changes: 32 additions & 0 deletions src/main/java/com/thealgorithms/maths/NeonNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thealgorithms.maths;

/**
* Neon Number algorithm.
* A number whose sum of digits of its square equals the number itself.
* Example: 9 - 9^2 = 81 - 8+1 = 9
*
* @see <a href="https://en.wikipedia.org/wiki/Recreational_mathematics">
* Wikipedia</a>
*/
public final class NeonNumber {

private NeonNumber() {
}

/**
* Check if a number is a Neon number.
*
* @param number the input number
* @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;

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 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;

digitSum = digitSum + temp % 10;
temp /= 10;
}
return digitSum == number;
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/thealgorithms/maths/NeonNumberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

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.

assertTrue(NeonNumber.isNeon(0));
assertTrue(NeonNumber.isNeon(1));
assertTrue(NeonNumber.isNeon(9));
}

@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));

assertFalse(NeonNumber.isNeon(2));
assertFalse(NeonNumber.isNeon(5));
assertFalse(NeonNumber.isNeon(10));
}
}
Loading