-
Notifications
You must be signed in to change notification settings - Fork 21k
Expand file tree
/
Copy pathNeonNumber.java
More file actions
39 lines (33 loc) · 952 Bytes
/
NeonNumber.java
File metadata and controls
39 lines (33 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.thealgorithms.maths;
/**
* This class checks whether a number is a Neon Number.
* A Neon number is a number where the sum of digits of
* its square is equal to the number itself.
*
* Example:
* 9^2 = 81 → 8 + 1 = 9 (Neon Number)
*/
public final class NeonNumber {
private NeonNumber() {
// Prevent instantiation
}
/**
* Checks if a number is a Neon Number.
*
* @param number the input number
* @return true if the number is a Neon Number, otherwise false
* @throws IllegalArgumentException if the number is negative
*/
public static boolean isNeon(int number) {
if (number < 0) {
throw new IllegalArgumentException("Number must be non-negative");
}
int square = number * number;
int sum = 0;
while (square > 0) {
sum += square % 10;
square /= 10;
}
return sum == number;
}
}