An ugly number is a positive integer which does not have a prime factor other than 2, 3, and 5.
You are given an integer n, return true if n is an ugly number.
Example 1:
Input: n = 4
Output: trueExplanation: 4 = 2 * 2.
Example 2:
Input: n = 1
Output: trueExplanation: 1 has no prime factors.
Example 3:
Input: n = 11
Output: falseExplanation: 11 is a prime number.
Constraints:
-(2^31) <= n <= ((2^31) - 1)Before attempting this problem, you should be comfortable with:
An ugly number has only 2, 3, and 5 as prime factors. This means if we keep dividing the number by 2, 3, and 5 (as long as it is divisible), we should eventually reach 1. If any other prime factor exists, the number will not reduce to 1.
n <= 0, return false (ugly numbers are positive).[2, 3, 5]:n is divisible by the prime, divide n by it.n == 1.true if n is 1, false otherwise.Ugly numbers are defined as positive integers. A common mistake is not checking for n <= 0 at the start, which can lead to infinite loops when trying to divide zero or incorrect results for negative numbers. Always return false immediately for non-positive inputs.
While the order of dividing by 2, 3, and 5 does not affect correctness, some implementations accidentally skip primes or use incorrect loop conditions. Ensure each prime factor is completely divided out before moving to the next one, using a while loop rather than an if statement.