-
-
Save andrewgazelka/15b95a2f5ace938cee9d9228b2f193bb to your computer and use it in GitHub Desktop.
This file contains 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
fun productSumFactors(max: Int) = sequence { | |
val size = log2(max.toDouble()).toInt() + 1 | |
val array = List(size) { 1 }.toIntArray() | |
while (true) { | |
var indexOn = array.size - 1 | |
array[indexOn]++ | |
var reduced: Int | |
while (array.reduce { acc, i -> acc * i }.also { reduced = it } > max) { | |
indexOn-- | |
if (indexOn < 0) return@sequence | |
val nextIndex = array[indexOn]++ | |
array[indexOn + 1] = nextIndex | |
} | |
val sum = array.sum() | |
val k = size + (reduced - sum) | |
yield(FactorResult(k, reduced)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment