Python for Beginners: Check for Nude Number in Python

You might have read about prime numbers, composite numbers, even numbers, odd numbers, and many more. But, Have you ever wondered what a Nude number is? In this article, we will discuss what a Nude number is. We will also discuss a python program to check if a number is a Nude number or not.

What Is A Nude Number?

A nude number is a number that is divisible by all of its non-zero digits i.e. If all the digits in a given number are factors of the number, the given number is called a nude number. 

For instance, 24 is divisible by both 2 and 4. Hence, 24 is a nude number. On the contrary, 26 is divisible by 2 but it is not divisible by 6. So, it is not a nude number.

image

Let us now try to develop an algorithm to check for a nude number.

Check for Nude Number in Python

To check whether a given number is a nude number or not, we will first have to extract all of its digits. After that, we need to divide the number by all of the digits. If a number is not divisible by any of its digits, we will say that the given number is not a nude number. Otherwise, the number will be declared a nude number. 

To extract the digits from the given number, we will first create a set to store all the digits. After that, we will start dividing the given number by 10. In each division, the rightmost digit of the number will be extracted as a remainder and will be stored in the set. We will continue dividing the number by 10 and we will store the remainder in the set till the number becomes 0. 

def create_digits(N):
    digits = set()
    while N > 0:
        digit = N % 10
        N = N // 10
        digits.add(digit)
    return digits


input_number = 12345
digits = create_digits(input_number)
print("Digits in {} are {}.".format(input_number,digits))

Output:

Digits in 12345 are {1, 2, 3, 4, 5}.

After extracting all the digits, we will divide the given number by each digit. If any digit is not a factor of the given number, we will declare the number is not a nude number. Always remember that we have to consider only non-zero digits during division. Otherwise, the program will run into errors.

The entire Python program to check for a nude number is as follows.

def create_digits(N):
    digits = set()
    while N > 0:
        digit = N % 10
        N = N // 10
        digits.add(digit)
    return digits


def is_nude(N):
    digits = create_digits(N)
    for digit in digits:
        if digit != 0 and N % digit != 0:
            return False
    return True


input_number = 12345
digits = create_digits(input_number)
output = is_nude(input_number)
print("Digits in {} are {}.".format(input_number, digits))
print("{} is a nude number:{}".format(input_number, output))
input_number = 24
output = is_nude(input_number)
digits = create_digits(input_number)
print("Digits in {} are {}.".format(input_number, digits))
print("{} is a nude number:{}".format(input_number, output))

Output:

Digits in 12345 are {1, 2, 3, 4, 5}.
12345 is a nude number:False
Digits in 24 are {2, 4}.
24 is a nude number:True

Conclusion

In this article, we have discussed what a nude number is. We have also discussed the algorithm to check if a given number is a nude number or not and implemented it in python. To learn more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Author: Shantun Parmar

Leave a Reply

Your email address will not be published.