/
ch-2.kt
63 lines (60 loc) · 1.13 KB
/
ch-2.kt
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import kotlin.math.*
fun divisors(n: Int): ArrayList<Int> {
var ff=ArrayList<Int>(1)
if (n==1) {
return ff
}
var s = Math.sqrt(n.toDouble()).toInt()
if (s * s == n) {
ff.add(s)
s -= 1
}
for (pf in 2..s) {
if (n % pf == 0) {
ff.add(pf)
ff.add(n / pf)
}
}
return ff
}
fun is_weird(n: Int): Boolean {
val dd=divisors(n)
if (dd.sum() <= n) {
return false
}
for (mask in 1..(1 shl dd.size)-1) {
var ss=0
for ((i,d) in dd.withIndex()) {
if (mask and (1 shl i) > 0) {
ss += d
if (ss > n) {
break
}
}
if (ss == n) {
return false
}
}
}
return true
}
fun main() {
if (!is_weird(12)) {
print("Pass")
} else {
print("FAIL")
}
print(" ")
if (!is_weird(13)) {
print("Pass")
} else {
print("FAIL")
}
print(" ")
if (is_weird(70)) {
print("Pass")
} else {
print("FAIL")
}
println("")
}