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, …
Input: M = 3, N = 9
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
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.
i 0 1 2 3 4 5 6 7 8 9 10 Fi 0 1 1 2 3 5 8 13 21 34 55 Fi mod 2 0 1 1 0 1 1 0 1 1 0 10
- 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:
Time Complexity: O(1), because this code runs almost 60 times for any input number.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Sum of Fibonacci Numbers in a range
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Array range queries to count the number of Fibonacci numbers with updates
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count numbers with unit digit k in given range
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count of Numbers in a Range where digit d occurs exactly K times
- Find the highest occurring digit in prime numbers in a range
- Sum of range in a series of first odd then even natural numbers
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- K- Fibonacci series
- n'th multiple of a number in Fibonacci Series
- Factorial of each element in Fibonacci series
- Nth term of a Custom Fibonacci series
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.