• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Check for Nude Number in Python

Check for Nude Number in Python

Author: Aditya Raj
Last Updated: December 23, 2021

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.

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.

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us