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, and 5.
Note that 1 is typically treated as an ugly number.
Assume: The input value will be an integer and within a typical 32-bit signed integer range.
Yes, the output should be a boolean: True
if the number is ugly; otherwise, False
.
False
since ugly numbers are positive.def is_ugly_number(num):
if num <= 0:
return False
for factor in [2, 3, 5]:
while num % factor == 0:
num //= factor
return num == 1
# Example Use Case
print(is_ugly_number(6)) # True
print(is_ugly_number(8)) # True
print(is_ugly_number(14)) # False
This solution should efficiently determine if a number is an “ugly number” using the defined constraints and properties.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?