Skip to content

Commit 63eb39b

Browse files
Add NeonNumber algorithm in maths package
1 parent 0a2c7f2 commit 63eb39b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* This class checks whether a number is a Neon Number.
5+
* A Neon number is a number where the sum of digits of
6+
* its square is equal to the number itself.
7+
*
8+
* Example:
9+
* 9^2 = 81 → 8 + 1 = 9 (Neon Number)
10+
*/
11+
public final class NeonNumber {
12+
13+
private NeonNumber() {
14+
// Prevent instantiation
15+
}
16+
17+
/**
18+
* Checks if a number is a Neon Number.
19+
*
20+
* @param number the input number
21+
* @return true if the number is a Neon Number, otherwise false
22+
* @throws IllegalArgumentException if the number is negative
23+
*/
24+
public static boolean isNeon(int number) {
25+
if (number < 0) {
26+
throw new IllegalArgumentException("Number must be non-negative");
27+
}
28+
29+
int square = number * number;
30+
int sum = 0;
31+
32+
while (square > 0) {
33+
sum += square % 10;
34+
square /= 10;
35+
}
36+
37+
return sum == number;
38+
}
39+
}

0 commit comments

Comments
 (0)