HomeLeetcode263. Ugly Number - Leetcode Solutions

263. Ugly Number – Leetcode Solutions

Description

An ugly number is a positive integer whose prime factors are limited to 23, and 5.

Given an integer n, return true if n is an ugly number.

Examples:

Example 1:

Input: n = 6
Output: true
Explanation: 6 = 2 × 3

Example 2:

Input: n = 1
Output: true
Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.

Example 3:

Input: n = 14
Output: false
Explanation: 14 is not ugly since it includes the prime factor 7.

Solution in Python

Python
class Solution:
    def isUgly(self, n: int) -> bool:
        # If the number is less than or equal to zero, it is not considered an ugly number
        if n <= 0:
            return False
        
        # Continuously divide the number by 2, 3, or 5 if it's divisible by these numbers
        for factor in [2, 3, 5]:
            while n % factor == 0:  # While the number is divisible by the current factor
                n //= factor         # Divide the number by the factor
                
        # If after removing all 2s, 3s, and 5s, n is reduced to 1, it's an ugly number
        return n == 1

Explanation:

  1. Initial Check: We first check if n is less than or equal to zero. If it is, the number cannot be ugly (since ugly numbers are positive integers), so we return False.
  2. Prime Factors (2, 3, 5): We then loop through the prime factors (2, 3, and 5). For each factor, we use a while loop to keep dividing n by that factor as long as it’s divisible. This removes all instances of 2, 3, and 5 from n.
  3. Final Check: After the loop finishes, if n has been reduced to 1, it means the original number only had 2, 3, and 5 as prime factors, so we return True. Otherwise, we return False.

Solution in C++

C++
class Solution {
public:
    bool isUgly(int n) {
        // If the number is less than or equal to zero, it is not considered an ugly number
        if (n <= 0) {
            return false;
        }
        
        // Continuously divide the number by 2, 3, or 5 if it's divisible by these numbers
        for (int factor : {2, 3, 5}) {
            while (n % factor == 0) { // While n is divisible by the current factor
                n /= factor;          // Divide n by the factor
            }
        }
        
        // If after removing all 2s, 3s, and 5s, n is reduced to 1, it's an ugly number
        return n == 1;
    }
};

Solution in Javascript

JavaScript
/**
 * @param {number} n
 * @return {boolean}
 */
var isUgly = function(n) {
    // If the number is less than or equal to zero, it is not considered an ugly number
    if (n <= 0) {
        return false;
    }
    
    // Array of prime factors we want to check: 2, 3, and 5
    const factors = [2, 3, 5];

    // Iterate through each prime factor
    for (let factor of factors) {
        // While n is divisible by the current factor
        while (n % factor === 0) {
            n /= factor; // Divide n by the factor to remove that factor completely
        }
    }
    
    // After removing all 2s, 3s, and 5s, if n is reduced to 1, it is an ugly number
    return n === 1;
};

Solution in Java

Java
class Solution {
    public boolean isUgly(int n) {
        // If the number is less than or equal to zero, it is not considered an ugly number
        if (n <= 0) {
            return false;
        }
        
        // Define an array of prime factors we want to check: 2, 3, and 5
        int[] factors = {2, 3, 5};

        // Iterate through each prime factor
        for (int factor : factors) {
            // While n is divisible by the current factor
            while (n % factor == 0) {
                n /= factor; // Divide n by the factor to remove that factor completely
            }
        }
        
        // After removing all 2s, 3s, and 5s, if n is reduced to 1, it is an ugly number
        return n == 1;
    }
}

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular