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 power of power under mod of a prime
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find value of y mod (2 raised to power x)
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Count number of solutions of x^2 = 1 (mod p) in given range
- How to compute mod of a big number?
- Divide two integers without using multiplication, division and mod operator | Set2
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- Trick for modular division ( (x1 * x2 .... xn) / b ) mod (m)
- Find next greater number with same set of digits
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Find Cube Pairs | Set 1 (A n^(2/3) Solution)
- Find Cube Pairs | Set 2 (A n^(1/3) Solution)
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)
- Find set of m-elements with difference of any two elements is divisible by k
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.