Last active
May 12, 2017 14:15
-
-
Save JorgeCastilloPrz/944449d17c6a3ca6f725072f3182825a to your computer and use it in GitHub Desktop.
factor calculator class for a blog post
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
class FactorCalculator { | |
val sumCache = hashMapOf<Int, Int>() | |
val factorCache = hashMapOf<Int, List<Int>>() | |
fun sumOfFactors(number: Int) = sumCache.getOrPut(number, { | |
factorsOf(number).sum() | |
}) | |
fun isFactor(number: Int, potential: Int) = number % potential == 0 | |
fun factorsOf(number: Int) = factorCache.getOrPut(number, { | |
(1 to number).toList().filter { isFactor(number, it) } | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment