Determine whether a number is ugly by checking if it can be reduced to 1 using only factors 2, 3, and 5.
Problem
A positive integer is called ugly if its prime factors are limited to 2, 3, and 5.
Given an integer n, return true if n is an ugly number, otherwise return false.
An ugly number must be positive.
Notes
1is considered ugly.- Any number less than or equal to
0is not ugly.
Intuition
You may repeatedly divide the number by 2, 3, and 5 while possible. If the final value becomes 1, the number is ugly.
Input Format
- A single integer
n.
Output Format
- Return
trueifnis an ugly number; otherwise returnfalse.
Constraints
nis an integer.nmay be positive, zero, or negative.- Treat
1as ugly. - Numbers with any prime factor other than
2,3, or5are not ugly.
Example 1
Input
n = 6
Output
true
Explanation
6 = 2 × 3, and its prime factors are only 2 and 3.
Example 2
Input
n = 14
Output
false
Explanation
14 = 2 × 7, and 7 is not allowed.
Show 1 more example
Example 3
Input
n = 1
Output
true
Explanation
1 is defined as an ugly number.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.