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.

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

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer :: n
    logical :: result

    ! Example 1
    n = 6
    result = isUgly(n)
    print '(L1)', result

    ! Example 2
    n = 1
    result = isUgly(n)
    print '(L1)', result

    ! Example 3
    n = 14
    result = isUgly(n)
    print '(L1)', result

contains

    function isUgly(n) result(res)
        implicit none
        integer, intent(in) :: n
        logical :: res
        integer :: i

        if (n < 0) then
            res = .false.
            return
        end if

        do i = 2, 5
            if (mod(n, i) == 0) then
                n = n / i
            else
                res = .false.
                return
            end if
        end do

        res = .true.
    end function isUgly
end program main
❌ Compiled
❌ Executed
❌ Correct
```
program ugly_number
    implicit none

    integer, parameter :: INT_MIN = -231
    integer, parameter :: INT_MAX = 231 - 1

    integer :: n
    logical :: is_ugly_number

    ! Read the input
    read(*,*) n

    ! Check if the number is an ugly number
    is_ugly_number = is_ugly_number(n)

    ! Print the result
    if (is_ugly_number) then
        print *, "Yes"
    else
        print *, "No"
    end if

contains

    function is_ugly_number(n) result(is_ugly)
        implicit none

        integer, intent(in) :: n
        logical :: is_ugly

        integer :: i
        integer :: factor

        is_ugly = .false.

        ! Check if the number is less than or equal to 0
        if (n <= 0) then
            return
        end if

        ! Check if the number is 1
        if (n == 1) then
            is_ugly = .true.
            return
        end if

        ! Check if the number is divisible by 2, 3, or 5
        do i = 2, 5
            factor = i
            do while (mod(n, factor) == 0)
                n = n / factor
                factor = factor * i
            end do
        end do

        ! Check if the number is 1
        if (n == 1) then
            is_ugly = .true.
        end if

    end function is_ugly_number

end program ugly_number
```
🌐 Data from online sources
def is_ugly(n: int) -> bool:
    if n <= 0:
        return False
    for factor in [2, 3, 5]:
        while n % factor == 0:
            n //= factor
    return n == 1

The algorithm checks if the number is less than or equal to 0 because only positive integers can be ugly. If the input number is divisible by 2, 3, or 5, we keep dividing it by the respective factor in a loop until it's no longer divisible by that factor. After processing all factors (2, 3, 5), if the remaining number is 1, then the input number had only prime factors of 2, 3, and 5, hence it is an ugly number. Otherwise, it's not an ugly number.

🌐 Data from online sources
bool isUgly(int n) {
    if (n <= 0) return false;
    while (n % 2 == 0) n /= 2;
    while (n % 3 == 0) n /= 3;
    while (n % 5 == 0) n /= 5;
    return n == 1;
}

The algorithm checks if the number is less than or equal to 0 because only positive integers can be ugly. If the input number is divisible by 2, 3, or 5, we keep dividing it by the respective factor in a loop until it's no longer divisible by that factor. After processing all factors (2, 3, 5), if the remaining number is 1, then the input number had only prime factors of 2, 3, and 5, hence it is an ugly number. Otherwise, it's not an ugly number.