Ugly Number LeetCode Solution

Problem – Ugly Number LeetCode Solution

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.

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.

Constraints:

  • -231 <= n <= 231 - 1

Ugly Number LeetCode Solution in C++

for (int i=2; i<6 && num; i++)
    while (num % i == 0)
        num /= i;
return num == 1;

Ugly Number LeetCode Solution in Python

for p in 2, 3, 5:
    while num % p == 0 < num:
        num /= p
return num == 1

Ugly Number LeetCode Solution in Java

for (int i=2; i<6 && num>0; i++)
    while (num % i == 0)
        num /= i;
return num == 1;
Ugly Number LeetCode Solution Review:

In our experience, we suggest you solve this Ugly Number LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Ugly Number LeetCode Solution

Find on Leetcode

Conclusion:

I hope this Ugly Number LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.