Created
May 2, 2019 13:57
-
-
Save ylegall/b940d0fb29fafff0e914c874e43b8f6e to your computer and use it in GitHub Desktop.
generate lordic numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* consider all positive integers starting at 2. | |
* add 2 to the results. cross off every 2nd remaining number. | |
* The next number is 3, add it to the results. cross off every 3rd remaining number. | |
* The next number is 5, add it to the results. cross off every 5th remaining number. | |
* The next number is 7, add it to the results. cross off every 7th remaining number. | |
* etc | |
*/ | |
private fun lordicNumbers(size: Int): List<Int> { | |
if (size <= 0) return emptyList() | |
val results = mutableListOf<Int>() | |
val counters = mutableListOf<Int>() | |
var next = 1 | |
outer@ do { | |
++next | |
for (i in counters.indices) { | |
val count = counters[i] | |
if (count == 1) { | |
counters[i] = results[i] | |
continue@outer | |
} else { | |
counters[i] = count - 1 | |
} | |
} | |
results.add(next) | |
counters.add(next) | |
} while (results.size < size) | |
return results | |
} | |
fun main(args: Array<String>) { | |
println(lordicNumbers(100)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment