- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find last digit of the given sequence for given n in Python
Suppose we have a value n. We have to find the last digit of sequence S. The equation of S is given below −
$$\sum_{i=0\: 2^{^{i}}\leqslant n}^{\alpha } \sum_{j=0}^{n} 2^{2^{^{i}+2j}}$$
So, if the input is like n = 2, then the output will be 6 because: here only i = 0 and i are valid, so
- S0 = 2^(2^0 + 0) + 2^(2^0 + 2) + 2^(2^0 + 4) = 42
- S1 = 2^(2^1 + 0) + 2^(2^1 + 2) + 2^(2^1 + 4) = 84 The sum is 42+84 = 126, so last digit is 6.
To solve this, we will follow these steps −
- total:= 0
- temp := 1
- while temp <= n, do
- total := total + (2^temp mod 10)
- temp := temp * 2
- total := total * (1 +(4 when n is odd otherwise 0)) mod 10
- total := total + (2^temp mod 10)
- return total
Example
Let us see the following implementation to get better understanding −
def solve(n): total= 0 temp = 1 while (temp <= n): total += pow(2, temp, 10) temp *= 2 total = total * (1 + (4 if n %2 ==1 else 0)) % 10 return total n = 2 print(solve(n))
Input
2
Output
6
Advertisements