Given a very large number N. The task is to find (1n + 2n + 3n + 4n) mod 5.
Input: N = 4
(1 + 16 + 81 + 256) % 5 = 354 % 5 = 4
Input: N = 7823462937826332873467731
Approach: (1n + 2n + 3n + 4n) mod 5 = (1n mod ?(5) + 2n mod ?(5) + 3n mod ?(5) + 4n mod ?(5)) mod 5.
This formula is correct because 5 is a prime number and it is coprime with 1, 2, 3, 4.
Know about ?(n) and modulo of large number
?(5) = 4, hence (1n + 2n + 3n + 4n) mod 5 = (1n mod 4 + 2n mod 4 + 3n mod 4 + 4n mod 4) mod 5
Below is the implementation of the above approach:
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Find K such that |A - K| = |B - K|
- Find 2^(2^A) % B
- Find the value of f(n) / f(r) * f(n-r)
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find two numbers from their sum and XOR
- Find (a^b)%m where 'b' is very large
- Find x and y satisfying ax + by = n
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find the other number when LCM and HCF given
- Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9
- Find larger of x^y and y^x
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Find Kth smallest value for b such that a + b = a | b
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.