Wednesday, March 13, 2019

LeetCode 263. Ugly Number

'''
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.
'''
class Solution:
    #52ms, 13.3MB
    def isUgly(self, num: int) -> bool:
        if num < 1: 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
        else: return False

No comments:

Post a Comment