Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Van-Eck-sequence/Kotlin/van-eck-sequence.kotlin
Normal file
23
Task/Van-Eck-sequence/Kotlin/van-eck-sequence.kotlin
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
fun main() {
|
||||
println("First 10 terms of Van Eck's sequence:")
|
||||
vanEck(1, 10)
|
||||
println("")
|
||||
println("Terms 991 to 1000 of Van Eck's sequence:")
|
||||
vanEck(991, 1000)
|
||||
}
|
||||
|
||||
private fun vanEck(firstIndex: Int, lastIndex: Int) {
|
||||
val vanEckMap = mutableMapOf<Int, Int>()
|
||||
var last = 0
|
||||
if (firstIndex == 1) {
|
||||
println("VanEck[1] = 0")
|
||||
}
|
||||
for (n in 2..lastIndex) {
|
||||
val vanEck = if (vanEckMap.containsKey(last)) n - vanEckMap[last]!! else 0
|
||||
vanEckMap[last] = n
|
||||
last = vanEck
|
||||
if (n >= firstIndex) {
|
||||
println("VanEck[$n] = $vanEck")
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue