➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Ugly Number

image

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.

So basically what this task is asking us to do is to find the prime factors of a given number and check if it’s factor are within the positive integer

Let’s say I have a number 6, the prime factors of 6 are 2 and 3

Since the prime factors are within the constraint factors then we return True

Let’s take another number 14 , the prime factors are 2 and 7

Since it isn’t within the constaint factors i.e 7 isn’t within the constraint factor then we return False

So my approach to solve this is to get the prime factors of a given integer n and check if it meets the constaint

To get the prime factors (Prime Factorization) we can follow this process:

With that we would have the prime factors of the integer

The time complexity of this process is dependent of the size of the integer

I would have used Fermat’s Factoring Method but the issue it’s designed to find two factors of a composite integer.

If the composite integer has more than two prime factors, we may need to apply the method multiple times to find pairs of factors iteratively until you have factored the number completely.

That process itself is dependent of the size of the integer so I’m going with the normal process

Here’s my solve script: link image