-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUglyNumber.kt
More file actions
28 lines (25 loc) · 747 Bytes
/
UglyNumber.kt
File metadata and controls
28 lines (25 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
*
* 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.
* [Source](https://leetcode.com/problems/ugly-number/)
*/
@UseCommentAsDocumentation
private fun isUgly(n: Int): Boolean {
if(n == 1) return true
if(n == 0) return false
var number = n
while (number % 2 == 0) number = number.shr(1)
while (number % 3 == 0) number /= 3
while (number % 5 == 0) number /= 5
return number == 1
}
fun main() {
isUgly(n = -2147483648) shouldBe false
isUgly(n = 6) shouldBe true
isUgly(n = 8) shouldBe true
isUgly(n = 14) shouldBe false
}