Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun main() {
- val n: Int = 40
- val start = System.currentTimeMillis()
- val fibs: List<Long> = fibonacci().take(n).toList()
- println(fibs)
- println(fibs.filter { it % 2 == 0L }.sum())
- println("TIME: " + ((System.currentTimeMillis() - start) / 1000.0) + "s")
- }
- private fun fibonacci() = sequence {
- var terms = Pair(1L, 2L)
- // this sequence is infinite
- while (true) {
- yield(terms.first)
- terms = Pair(terms.second, terms.first + terms.second)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement