Advertisement
ttaaa

fb_kotlin

Dec 7th, 2021
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.51 KB | None | 0 0
  1. fun main() {
  2.     val n: Int = 40
  3.  
  4.     val start = System.currentTimeMillis()
  5.     val fibs: List<Long> = fibonacci().take(n).toList()
  6.  
  7.     println(fibs)
  8.     println(fibs.filter { it % 2 == 0L }.sum())
  9.     println("TIME: " + ((System.currentTimeMillis() - start) / 1000.0) + "s")
  10. }
  11.  
  12. private fun fibonacci() = sequence {
  13.     var terms = Pair(1L, 2L)
  14.     // this sequence is infinite
  15.     while (true) {
  16.         yield(terms.first)
  17.         terms = Pair(terms.second, terms.first + terms.second)
  18.     }
  19. }
  20.  
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement