Skip to content

Instantly share code, notes, and snippets.

@siosio
Created July 17, 2012 03:32
Show Gist options
  • Save siosio/3126861 to your computer and use it in GitHub Desktop.
Save siosio/3126861 to your computer and use it in GitHub Desktop.
Kotlinでフィボナッチ数列的な
import java.util.Iterator
fun main(args: Array<String>) {
fibonacci(1000).forEach {println(it)}
}
fun fibonacci(max: Int): Iterator<Int> {
var current = #(1, 1)
return iterate<Int> {
val next = current._1 + current._2
if (next < max) {
current = #(current._2, next)
next
} else {
null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment