Neon numbers in a range in Python

In this tutorial, given a range of numbers, our task is to print neon numbers in that range. We will see this along with its implementation in Python.

Understanding Neon Numbers

We call a number as a neon number if the sum of the digits in the square of that number is equal to that number.

For example, let us consider the number 9.
Square of 9 = 81.
Sum of digits in the square of 9 = 8 + 1 =9

Here, the sum of digits in the square of 9 is equal to 9. So, 9 is called a neon number.

Consider another number 7.
Square of 7 = 49.
Sum of digits in the square of 7 = 4 + 9 =13

Here, the sum of digits in the square of 7 is not equal to 7. So, 7 is not called a neon number.

Implementation in Python

First, let us get the lower and upper bound of the range from the user.

print ("Enter the lower bound of the range")
lower_bound = int(input())
print ("Enter the upper bound of the range")
upper_bound = int(input())

Now, let us define a function that checks if the given number is a neon number or not. And this function returns True if it is a neon number else False.

def neon_or_not (num) :
    square = num * num 
    sum = 0
    while (square != 0) : 
        sum = sum + (square % 10)
        square = square // 10
    c = (sum == num)
    return c

In this function, we are first performing the square of the number and then we are performing the sum of the digits in the square. Note that, here we are performing integer division(//). Next, we are checking if both the sum and the original number are the same and we are returning True if both are the same else we are returning False.

In the given range, let us print the neon numbers.

i = lower_bound
print ("Neon numbers between",lower_bound,"and",upper_bound,"are :")
while i <= upper_bound : 
  if (neon_or_not(i)) : 
    print(i)
  i = i + 1

Therefore, we get all the neon numbers in the given range.

Output:

Enter the lower bound of the range

1
Enter the upper bound of the range

10
Neon numbers between 1 and 10 are :
1
9

Also read:

Check a number is Narcissistic number or not in Python
Print all Disarium numbers within given range in Python

Leave a Reply

Your email address will not be published. Required fields are marked *