0
0
PythonProgramBeginner · 2 min read

Python Program to Find Neon Number

A neon number is a number where the sum of digits of its square equals the number itself; in Python, you can check it by squaring the number, summing the digits of the square, and comparing with the original number using code like sum(int(d) for d in str(num*num)) == num.
📋

Examples

Input9
Output9 is a neon number
Input5
Output5 is not a neon number
Input1
Output1 is a neon number
🧠

How to Think About It

To find if a number is neon, first square the number. Then, add all the digits of this squared value. If this sum equals the original number, it is a neon number; otherwise, it is not.
📐

Algorithm

1
Get input number from the user
2
Calculate the square of the number
3
Convert the square to a string to access each digit
4
Sum all digits of the squared number
5
Compare the sum with the original number
6
Print if the number is neon or not based on the comparison
💻

Code

python
num = int(input("Enter a number: "))
square = num * num
sum_digits = sum(int(d) for d in str(square))
if sum_digits == num:
    print(f"{num} is a neon number")
else:
    print(f"{num} is not a neon number")
Output
Enter a number: 9 9 is a neon number
🔍

Dry Run

Let's trace the number 9 through the code

1

Input number

num = 9

2

Calculate square

square = 9 * 9 = 81

3

Sum digits of square

digits of 81 are '8' and '1', sum = 8 + 1 = 9

4

Compare sum with original

sum_digits = 9, num = 9, so they are equal

5

Print result

"9 is a neon number"

StepValue
Input number9
Square81
Sum of digits9
Comparison9 == 9 (True)
💡

Why This Works

Step 1: Square the number

We calculate num * num to get the square, which is the base for checking the neon property.

Step 2: Sum digits of the square

By converting the square to a string, we can iterate over each digit and sum them using sum(int(d) for d in str(square)).

Step 3: Compare sum with original number

If the sum of digits equals the original number, it confirms the number is neon; otherwise, it is not.

🔄

Alternative Approaches

Using while loop to sum digits
python
num = int(input("Enter a number: "))
square = num * num
sum_digits = 0
while square > 0:
    sum_digits += square % 10
    square //= 10
if sum_digits == num:
    print(f"{num} is a neon number")
else:
    print(f"{num} is not a neon number")
This method uses arithmetic operations instead of string conversion, which can be more efficient for large numbers.
Using a function to check neon number
python
def is_neon(num):
    square = num * num
    return sum(int(d) for d in str(square)) == num

num = int(input("Enter a number: "))
if is_neon(num):
    print(f"{num} is a neon number")
else:
    print(f"{num} is not a neon number")
Encapsulating logic in a function improves code reuse and readability.

Complexity: O(d) time, O(d) space

Time Complexity

The time depends on the number of digits d in the squared number because we sum each digit once.

Space Complexity

Space is used to store the string representation of the square, which is proportional to the number of digits d.

Which Approach is Fastest?

Using arithmetic operations (modulus and division) avoids string conversion and can be faster and use less memory for very large numbers.

ApproachTimeSpaceBest For
String conversionO(d)O(d)Simplicity and readability
Arithmetic operationsO(d)O(1)Performance with large numbers
Function encapsulationO(d)O(d)Code reuse and clarity
💡
Use string conversion to easily sum digits of a number in Python.
⚠️
Forgetting to compare the sum of digits of the square with the original number instead of the square itself.