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
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
temp.f95:37:16: 37 | n = n / i | 1 Error: Dummy argument βnβ with INTENT(IN) in variable definition context (assignment) at (1)
```
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
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:26:28: 26 | function is_ugly_number(n) result(is_ugly) | 1 Error: Procedure βis_ugly_numberβ at (1) has an explicit interface from a previous declaration temp.f95:27:21: 27 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:29:32: 29 | integer, intent(in) :: n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:30:26: 30 | logical :: is_ugly | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:32:20: 32 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:33:25: 33 | integer :: factor | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:35:25: 35 | is_ugly = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:38:24: 38 | if (n <= 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:39:18: 39 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:40:11: 40 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:43:24: 43 | if (n == 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:44:28: 44 | is_ugly = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:45:18: 45 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:46:11: 46 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:49:19: 49 | do i = 2, 5 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:50:22: 50 | factor = i | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:51:42: 51 | do while (mod(n, factor) == 0) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:52:30: 52 | n = n / factor | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:53:35: 53 | factor = factor * i | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:54:15: 54 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:55:11: 55 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:58:24: 58 | if (n == 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:59:28: 59 | is_ugly = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:60:11: 60 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:62:7: 62 | end function is_ugly_number | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:65:2: 65 | ``` | 1 Error: Invalid character in name at (1)
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.
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.