Check whether an integer has no prime factors other than 2, 3 and 5

Quick Overview

Decide whether a signed 32-bit integer is an ugly number, meaning a positive integer with no prime factors other than 2, 3 and 5. Tests basic number theory, careful handling of 1, zero and negative inputs, and reasoning about how many division steps the check needs.

Check whether an integer has no prime factors other than 2, 3 and 5

Company: GitHub

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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. ### Function Signature ```python def is_ugly(n: int) -> bool: ``` ### Rules - `1` is an ugly number, because it has no prime factors at all. - Zero and negative numbers are never ugly, since an ugly number must be positive. ### Constraints - `-2^31 <= n <= 2^31 - 1` ### Examples ```text Input: n = 6 Output: True ``` `6 = 2 x 3`, and both 2 and 3 are allowed prime factors. ```text Input: n = 14 Output: False ``` `14 = 2 x 7`. The factor 7 is not one of 2, 3 or 5, so 14 is not ugly. ```text Input: n = 1 Output: True ``` 1 has no prime factors, so none of them falls outside 2, 3 and 5.

Overview: Decide whether a signed 32-bit integer is an ugly number, meaning a positive integer with no prime factors other than 2, 3 and 5. Tests basic number theory, careful handling of 1, zero and negative inputs, and reasoning about how many division steps the check needs.

GitHub logo
GitHub
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.

Function Signature

def is_ugly(n: int) -> bool:

Rules

  • 1 is an ugly number, because it has no prime factors at all.
  • Zero and negative numbers are never ugly, since an ugly number must be positive.

Constraints

  • -2^31 <= n <= 2^31 - 1

Examples

Input: n = 6
Output: True

6 = 2 x 3, and both 2 and 3 are allowed prime factors.

Input: n = 14
Output: False

14 = 2 x 7. The factor 7 is not one of 2, 3 or 5, so 14 is not ugly.

Input: n = 1
Output: True

1 has no prime factors, so none of them falls outside 2, 3 and 5.