Last digit of sum of numbers in the given range in the Fibonacci series | Java program to calculate last digit of the sum of the fibonacci numbers from M to N ; 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
import java . util . * ; class GFG { static int fib ( long n ) { int f0 = 0 ; int f1 = 1 ; if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; else { int rem = ( int ) ( n % 60 ) ; if ( rem == 0 ) return 0 ; for ( int i = 2 ; i < rem + 3 ; i ++ ) { int f = ( f0 + f1 ) % 60 ; f0 = f1 ; f1 = f ; } int s = f1 - 1 ; return s ; } } public static void main ( String args [ ] ) { int m = 10087887 ; long n = 2983097899L ; int Final = ( int ) Math . abs ( fib ( n ) - fib ( m - 1 ) ) ; System . out . println ( Final % 10 ) ; } }