Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-132

Ugly Number

Problem

Return True if n is an ugly number, meaning its only prime factors are 2, 3 and 5, and False otherwise.

Repeatedly divide n by each of 2, 3 and 5 while possible, then check whether exactly 1 remains. Values less than or equal to 0 are not ugly.

Hint

After removing all factors 2, 3 and 5, an ugly number is exactly 1.

Test cases

case 1
in: [6]
expected: true
case 2
in: [8]
expected: true
case 3
in: [14]
expected: false
case 4
in: [1]
expected: true
case 5
in: [0]
expected: false
Starter codePython
def ugly_number(n):
    # Your code here
    pass

5 test cases · run your code against them in the editor