Last digit of sum of numbers in the given range in the Fibonacci series | Calculate the sum of the first N Fibonacci numbers using Pisano period ; The first two Fibonacci numbers ; Base case ; Pisano period for % 10 is 60 ; Checking the remainder ; The loop will range from 2 to two terms after the remainder ; Driver Code
function fib ( n ) { var f0 = 0 ; var f1 = 1 ; if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; else { var rem = parseInt ( ( n % 60 ) ) ; if ( rem == 0 ) return 0 ; for ( i = 2 ; i < rem + 3 ; i ++ ) { var f = ( f0 + f1 ) % 60 ; f0 = f1 ; f1 = f ; } var s = f1 - 1 ; return s ; } } var m = 10087887 ; var n = 2983097899 ; var Final = parseInt ( Math . abs ( fib ( n ) - fib ( m - 1 ) ) ) ; document . write ( Final % 10 ) ;