
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check a number is ugly number or not in Python
Suppose we have a number n, we have to check whether its prime factors only include 2, 3 or 5 or not.
So, if the input is like n = 18, then the output will be True, as 18's prime factors are 2 and 3.
To solve this, we will follow these steps −
- if n < 0, then
- return False
- factor := a list with elements [2,3,5]
- for each i in factor, do
- while n mod i is same as 0, do
- n := n / i
- while n mod i is same as 0, do
- return true when n is same as 1, otherwise false
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, n): if n < 0: return False factor = [2,3,5] for i in factor: while n%i ==0: n/=i return n==1 ob = Solution() print(ob.solve(18))
Input
18
Output
True
- Related Articles
- How To Check Whether a Number is an Ugly Number or Not in Java?
- Program to check whether given number is Narcissistic number or not in Python
- Python program to check if a number is Prime or not
- Python program to check a number n is weird or not
- Program to check a number is power of two or not in Python
- Swift Program to Check If a Number is Spy number or not
- Program to check whether a number is Proth number or not in C++
- Python program to check credit card number is valid or not
- Check if a number is an Achilles number or not in Python
- Check if given number is Emirp Number or not in Python
- Program to check whether every rotation of a number is prime or not in Python
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C# Program to check if a number is prime or not
- C Program to Check Whether a Number is Prime or not?

Advertisements