Why 2 is not an ugly number


  • -1
    X

    I use the following code. It show that 2 is not the ugly number.
    class Solution {
    public:
    bool isUgly(int num) {
    // Only num is positive
    if (num > 0) {
    // 1 is typically treated as an ugly number
    if (num == 1)
    return true;
    // Get all prime factors
    vector<int> v = getAllPrimeFactors(num);
    for(int i = 0; i < v.size(); i++) {
    // Handles prime factors only include 2,3,5
    if(v.at(i) != 2 or v.at(i) != 3 or v.at(i) !=5)
    return false;
    }
    return true;
    }
    return false;
    }
    vector<int> getAllPrimeFactors (int num) {
    vector<int> v;
    // If num is even
    if (num % 2 == 0)
    v.push_back(2);
    // Get rid of 2 as prime factors
    while(num % 2 == 0) {
    num = num / 2;
    }
    for (int i = 3; i < sqrt(num); i = i + 2) {
    if (num % i == 0)
    v.push_back (i);
    while (num % i == 0) {
    num = num / i;
    }
    }
    // This condition is to handle if the num is a prime number.
    // Greater than 2
    if (num > 2)
    v.push_back(num);
    return v;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.