Project Euler #2: Even Fibonacci numbers

  • + 0 comments

    KOTLIN Code

    fun main(args: Array<String>) {
        val t = readLine()!!.trim().toInt()
    
        for (tItr in 1..t) {
            val n = readLine()!!.trim().toLong()
            createFibonacciSum(n)
        } 
    }
    
    private fun createFibonacciSum(n: Long){
        var left = 0L
        var middle = 1L
        var sum = 0L
        var i = 0
        while(i< n && (middle + left)< n){
            var right = middle + left
            left = middle
            middle = right
            if(right % 2L == 0L) sum += right
            i++ 
        }
        println(sum)
    }