Skip to content

Instantly share code, notes, and snippets.

@JorgeCastilloPrz
Last active May 12, 2017 14:15
Show Gist options
  • Save JorgeCastilloPrz/944449d17c6a3ca6f725072f3182825a to your computer and use it in GitHub Desktop.
Save JorgeCastilloPrz/944449d17c6a3ca6f725072f3182825a to your computer and use it in GitHub Desktop.
factor calculator class for a blog post
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