Skip to content

Instantly share code, notes, and snippets.

@ylegall
Created May 2, 2019 13:57
Show Gist options
  • Save ylegall/b940d0fb29fafff0e914c874e43b8f6e to your computer and use it in GitHub Desktop.
Save ylegall/b940d0fb29fafff0e914c874e43b8f6e to your computer and use it in GitHub Desktop.
generate lordic numbers
/**
* 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