ttaaa

fb_kotlin

Dec 7th, 2021
495
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data