Skip to main content
Back to problems
Leetcode
Easy
Math
Number Theory
Ugly Number

Determine whether a number is ugly by checking if it can be reduced to 1 using only factors 2, 3, and 5.

Acceptance 0%
Problem Statement

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

  • 1 is considered ugly.
  • Any number less than or equal to 0 is 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 true if n is an ugly number; otherwise return false.

Constraints

  • n is an integer.
  • n may be positive, zero, or negative.
  • Treat 1 as ugly.
  • Numbers with any prime factor other than 2, 3, or 5 are not ugly.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.