Last digit of sum of numbers in the given range in the Fibonacci series | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long fib ( long long n ) { long long f0 = 0 ; long long f1 = 1 ; if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; else { long long rem = n % 60 ; if ( rem == 0 ) return 0 ; for ( long long i = 2 ; i < rem + 3 ; i ++ ) { long long f = ( f0 + f1 ) % 60 ; f0 = f1 ; f1 = f ; } long long s = f1 - 1 ; return s ; } } int main ( ) { long long m = 10087887 ; long long n = 2983097899 ; long long final = abs ( fib ( n ) - fib ( m - 1 ) ) ; cout << final % 10 << endl ; }