An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
Given an integer n, return true if n is an ugly number, and false otherwise.
Note that 1 is considered ugly (it has no prime factors at all).
We use analytics and advertising cookies to understand how the site is used and whether our ads on Facebook and Instagram work. They are set only if you accept. See our Privacy Policy for details.
An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
Given an integer n, return true if n is an ugly number, and false otherwise.
Note that 1 is considered ugly (it has no prime factors at all).
An easy math problem, graded against 7 test cases (4 of them hidden).
Number theory, digit manipulation, and closed-form reasoning.
Reach for it when you see: Primes, digits, base conversion, or a problem with a formula hiding behind it.
More Math problems →Strip every factor of 2, then 3, then 5 with repeated integer division. If only those primes were present, the remainder collapses to 1; any leftover value greater than 1 must contain a forbidden prime. Non-positive inputs are rejected up front since ugly numbers are positive by definition.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.