Related Articles

# Last digit of sum of numbers in the given range in the Fibonacci series

• Difficulty Level : Medium
• Last Updated : 26 Mar, 2021

Given two non-negative integers M, N which signifies the range [M, N] where M ≤ N, the task is to find the last digit of the sum of FM + FM+1… + FN where FK is the Kth Fibonacci number in the Fibonacci series

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

Examples:

Input: M = 3, N = 9
Output:
Explanation:
We need to find F3 + F4 + F5 + F6 + F7 + F8 + F9
=> 2 + 3 + 5 + 8 + 13 + 21 + 34 = 86.
Clearly, the last digit of the sum is 6.
Input: M = 3, N = 7
Output:
Explanation:
We need to find F3 + F4 + F5 + F6 + F7
=> 2 + 3 + 5 + 8 + 13 = 31.
Clearly, the last digit of the sum is 1.

Naive Approach: The naive approach for this problem is to one by one find the sum of all Kth Fibonacci Numbers where K lies in the range [M, N] and return the last digit of the sum in the end. The time complexity for this approach is O(N) and this method fails for higher-ordered values of N.
Efficient Approach: An efficient approach for this problem is to use the concept of Pisano Period

• The idea is to calculate the sum of (M – 1) and N Fibonacci numbers respectively, and subtracting the last digit of the computed values.
• This is because the last digit of the sum of all the Kth Fibonacci numbers such that K lies in the range [M, N] is equal to the difference of the last digits of the sum of all the Kth Fibonacci numbers in the range [0, N] and the sum of all the Kth Fibonacci numbers in the range [0, M – 1].
• These values can respectively be calculated by the concept of the Pisano period in a very short time.
• Let’s understand how the Pisano period works. The following table illustrates the first 10 Fibonacci numbers along with its values obtained when modulo 2 is performed on the numbers.

•
• Clearly, the Pisano period for (Fi mod 2) is 3 since 011 repeat itself and length(011) = 3.
• Now, lets observe the following identity:

7 = 2 * 3 + 1
Dividend = (Quotient × Divisor) + Remainder
=> F7 mod 2 = F1 mod 2 = 1.

• Therefore, instead of calculating the last digit of the sum of all numbers in the range [0, N], we simply calculate the sum until the remainder given that the Pisano period for Fi mod 10 is 60.

Below is the implementation of the above approach:

## C++

 `// C++ program to calculate``// last digit of the sum of the``// fibonacci numbers from M to N``#include``using` `namespace` `std;` `// Calculate the sum of the first``// N Fibonacci numbers using Pisano``// period``long` `long` `fib(``long` `long` `n)``{``    ` `    ``// The first two Fibonacci numbers``    ``long` `long` `f0 = 0;``    ``long` `long` `f1 = 1;` `    ``// Base case``    ``if` `(n == 0)``        ``return` `0;``    ``if` `(n == 1)``        ``return` `1;``    ``else``    ``{``        ``// Pisano period for % 10 is 60``        ``long` `long` `rem = n % 60;` `        ``// Checking the remainder``        ``if``(rem == 0)``           ``return` `0;` `        ``// The loop will range from 2 to``        ``// two terms after the remainder``        ``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;``    ``}``}` `// Driver Code``int` `main()``{``    ``long` `long` `m = 10087887;``    ``long` `long` `n = 2983097899;` `    ``long` `long` `final = ``abs``(fib(n) - fib(m - 1));``    ``cout << final % 10 << endl;``}` `// This code is contributed by Bhupendra_Singh`

## Java

 `// Java program to calculate``// last digit of the sum of the``// fibonacci numbers from M to N``import` `java.util.*;` `class` `GFG{` `// Calculate the sum of the first``// N Fibonacci numbers using Pisano``// period``static` `int` `fib(``long` `n)``{``    ` `    ``// The first two Fibonacci numbers``    ``int` `f0 = ``0``;``    ``int` `f1 = ``1``;` `    ``// Base case``    ``if` `(n == ``0``)``        ``return` `0``;``    ``if` `(n == ``1``)``        ``return` `1``;``    ``else``    ``{``        ` `        ``// Pisano period for % 10 is 60``        ``int` `rem = (``int``) (n % ``60``);` `        ``// Checking the remainder``        ``if``(rem == ``0``)``        ``return` `0``;` `        ``// The loop will range from 2 to``        ``// two terms after the remainder``        ``for``(``int` `i = ``2``; i < rem + ``3``; i++)``        ``{``           ``int` `f = (f0 + f1) % ``60``;``           ``f0 = f1;``           ``f1 = f;``        ``}``        ` `        ``int` `s = f1 - ``1``;``        ``return` `s;``    ``}``}` `// Driver Code``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``);``}``}` `// This code is contributed by AbhiThakur`

## Python3

 `# Python3 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``def` `fib(n):` `    ``# The first two Fibonacci numbers``    ``f0 ``=` `0``    ``f1 ``=` `1` `    ``# Base case``    ``if` `(n ``=``=` `0``):``        ``return` `0``    ``if` `(n ``=``=` `1``):``        ``return` `1``    ``else``:` `        ``# Pisano Period for % 10 is 60``        ``rem ``=` `n ``%` `60` `        ``# Checking the remainder``        ``if``(rem ``=``=` `0``):``            ``return` `0` `        ``# The loop will range from 2 to``        ``# two terms after the remainder``        ``for` `i ``in` `range``(``2``, rem ``+` `3``):``            ``f ``=``(f0 ``+` `f1)``%` `60``            ``f0 ``=` `f1``            ``f1 ``=` `f` `        ``s ``=` `f1``-``1``        ``return``(s)` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``m ``=` `10087887``    ``n ``=` `2983097899` `    ``final ``=` `fib(n)``-``fib(m``-``1``)` `    ``print``(final ``%` `10``)`

## C#

 `// C# program to calculate``// last digit of the sum of the``// fibonacci numbers from M to N``using` `System;` `class` `GFG{` `// Calculate the sum of the first``// N fibonacci numbers using Pisano``// period``static` `int` `fib(``long` `n)``{``    ` `    ``// The first two fibonacci numbers``    ``int` `f0 = 0;``    ``int` `f1 = 1;` `    ``// Base case``    ``if` `(n == 0)``        ``return` `0;``    ``if` `(n == 1)``        ``return` `1;``    ``else``    ``{``        ` `        ``// Pisano period for % 10 is 60``        ``int` `rem = (``int``)(n % 60);` `        ``// Checking the remainder``        ``if``(rem == 0)``           ``return` `0;` `        ``// The loop will range from 2 to``        ``// two terms after the remainder``        ``for``(``int` `i = 2; i < rem + 3; i++)``        ``{``           ``int` `f = (f0 + f1) % 60;``           ``f0 = f1;``           ``f1 = f;``        ``}``        ` `        ``int` `s = f1 - 1;``        ``return` `s;``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `m = 10087887;``    ``long` `n = 2983097899L;``    ``int` `Final = (``int``)Math.Abs(fib(n) -``                              ``fib(m - 1));``    ` `    ``Console.WriteLine(Final % 10);``}``}` `// This code is contributed by Code_Mech`

## Javascript

 ``
Output:
`5`

Time Complexity: O(1), because this code runs almost 60 times for any input number.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up