Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

Example 1:


Input: 6
Output: true
Explanation: 6 = 2 × 3

Example 2:


Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2


Example 3:


Input: 14
Output: false 
Explanation: 14 is not ugly since it includes another prime factor 7.


Note:


       1 is typically treated as an ugly number.
       Input is within the 32-bit signed integer range: [−231,  231 − 1].

Difficulty: introductory

Starter code:

class Solution:
    def isUgly(self, num: int) -> bool:
        

Solutions:
class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         if num < 1:
             return False
         while num % 2 == 0:
             num = num / 2
         while num % 3 == 0:
             num = num / 3
         while num % 5 == 0:
             num = num / 5
         if num == 1:
             return True
         else:
             return False

class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         if num == 0:
             return False
         for d in (5, 3, 2):
             while num % d == 0:
                 num //= d
             if num == 1:
                 break
         else:
             return False
         return True


class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         if num == 0:
             return False
         while num % 2 == 0:
             num /= 2
         while num % 3 == 0:
             num /= 3 
         while num % 5 == 0:
             num /= 5
         if num == 1:
             return True
         return False


class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         n = num
         if n <1:
             return False
         elif n == 1:
             return True
         else:
             while n >= 1:
                 ncount = 0
                 if n % 3 == 0:
                     n = n // 3
                     ncount  = 1
                 if n % 2 == 0:
                     n //= 2
                     ncount = 1
                 if n % 5 == 0:
                     n //= 5
                     ncount = 1
                 if ncount == 1:
                     pass
                 else:
                     break
             if n == 1:
                 return True
             else:
                 return False

class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         if num==0:
             return False
         while num%2==0:
             num=num/2
         while num%3==0:
             num=num/3
         while num%5==0:
             num=num/5
         if num==1:
             return True
         else:
             return False

class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         if num<1:
             return False
         if num==1:
             return True
         while num%2==0 or num%3==0 or num%5==0:
             if num%2==0:
                 num/=2
             elif num%3==0:
                 num/=3
             else:
                 num/=5
             if num==1:
                 return True
         return False


class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         lst = []
         if num < 1:
             return False
         if num == 1:
             return True
         i = 2
         while (i<num+1):
             print(num,i,' ',lst)
             if num%i == 0:
                 if i not in [2,3,5]:
                     lst.append(i)
                     if lst != []:
                         return False
                 num = num//i
             else:
                 i += 1
                 if i>5:
                     return False
         if lst == []:
             return True

class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         if num<=0:
             return False
         for i in [2,3,5]:
             while num%i==0:
                 num=num//i
         return num==1

class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         
         ## take care of trivial cases
         if num <= 0:
             return False
         elif num == 1:
             return True
         
         for factor in 5,3,2:
             while num % factor == 0:
                 num = num / factor
                 print (num)
         return int(num) == 1
         
         
         


class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         for p in [2,3,5]:
             while num%p == 0 < num:
                 num /= p
         return num==1

class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         if num<=0:
             return False
         count=0
         while(count==0):
             num=num/2
             if str(num)[-1]!='0':
                 print(11)
                 count=1
                 num=num*2
         count=0
         while (count==0):
             num=num/3
             if str(num)[-1]!='0':
                 count=1
                 num=num*3        
         count=0
         while (count==0):
             num=num/5
             if str(num)[-1]!='0':
                 count=1
                 num=num*5
         if num!=1.0:
             return False
         else:
             return True

class Solution:
     def isUgly(self, num):
         """
         :type num: int
         :rtype: bool
         """
         
         
    #    public CONCISE solution....
 
         if num <= 0:
             return False
         while num % 2 == 0:
             num //= 2
         while num % 3 == 0:
             num //= 3
         while num % 5 == 0:
             num //= 5
         return num == 1
         
         
 
         
         """
    #    my solution......a bit too slow....
    
         if num <= 0:
             return False
         if num in [1,2,3,4,5,6]:
             return True
         if self.isPrime(num):
             return False
         else:
             i = 2
             while i <= int(math.sqrt(num)):
                 if num%i == 0:
                     if (self.isPrime(i) and i not in [2,3,5]) or (self.isPrime(num//i) and num//i not in [2,3,5]):
                         return False
                 i += 1
             return True
             
         
         
     def isPrime(self, n):
         if n <= 1:
             return False
         for i in range(2, int(math.sqrt(n)) + 1):
             if n % i == 0:
                 return False
         return True
         """