Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Population-count/Kotlin/population-count.kotlin
Normal file
40
Task/Population-count/Kotlin/population-count.kotlin
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun popCount(n: Long) = when {
|
||||
n < 0L -> throw IllegalArgumentException("n must be non-negative")
|
||||
else -> java.lang.Long.bitCount(n)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println("The population count of the first 30 powers of 3 are:")
|
||||
var pow3 = 1L
|
||||
for (i in 1..30) {
|
||||
print("${popCount(pow3)} ")
|
||||
pow3 *= 3L
|
||||
}
|
||||
println("\n")
|
||||
println("The first thirty evil numbers are:")
|
||||
var count = 0
|
||||
var i = 0
|
||||
while (true) {
|
||||
val pc = popCount(i.toLong())
|
||||
if (pc % 2 == 0) {
|
||||
print("$i ")
|
||||
if (++count == 30) break
|
||||
}
|
||||
i++
|
||||
}
|
||||
println("\n")
|
||||
println("The first thirty odious numbers are:")
|
||||
count = 0
|
||||
i = 1
|
||||
while (true) {
|
||||
val pc = popCount(i.toLong())
|
||||
if (pc % 2 == 1) {
|
||||
print("$i ")
|
||||
if (++count == 30) break
|
||||
}
|
||||
i++
|
||||
}
|
||||
println()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue