This article is participating in the nuggets team number online activity, click to see the dachang spring recruiting positions

I. Title Description:

The original link

Give you an integer n, please determine whether n is ugly. If so, return true; Otherwise, return false.

Ugly numbers are positive integers that contain only prime factors 2, 3, and/or 5.

Example 1:

Input: n = 6 Output: true Description: 6 = 2 x 3Copy the code

Ii. Analysis of Ideas:

This is actually a simple math problem, according to the definition of ugly number, we only need to look at the prime factors of a number is only 2, 3, 5 can determine whether it is ugly number. Use the program to achieve it is also very simple, the number numnumnum 2, 3, 5 factors all get rid of, and then see if the result is 1, if 1 is an ugly number, not that there are other prime factors, not ugly number.

Iii. AC Code:

class Solution {
public:
    bool isUgly(int n) {
        if (n <= 0) return false;
        while (n % 2= =0 || n % 3= =0 || n % 5= =0) {
            if (n % 2= =0) n /= 2;
            else if (n % 3= =0) n /= 3;
            else n /= 5;
        }
        return n == 1; }};Copy the code

Iv. Summary:

In number theory, we often ask for the prime factors of a number. This problem only asks us to judge the prime factors of 2, 3 and 5. It is relatively simple.