You are given a function f(n) = (n1 + n2 + n3 + n4), you have to find the value of f(n) mod 5 for any given value of positive integer n.
Note: n may be large enough, such that f(n) > 1018.
Input : n = 4 Output : 0 Explanation : f(4) = 4 + 16 + 64 + 256 = 330, f(4) mod 5 = 330 mod 5 = 0. Input : n = 1 Output : 4 Explanation : f(1) = 1 + 1 + 1 + 1 = 4, f(1) mod 5 = 4.
First of all for solving this approach you may find the value of (n1 + n2 + n3 + n4) mod 5 directly with the help of anypower function and modulo operator.
But For the larger value of n, your result will be wrong because for large n value of f(n) may go out of range from long long int in that case you have to opt some other efficient way.
To solve this question lets do some small mathematical derivation for f(n).
f(n) = (n1 + n2 + n3 + n4) = (n) (n+1) (n2+1) Now, for finding f(n) mod 5 we must take care of unit digit of f(n) only, also as f(n) mod 5 is dependent on n%5, (n+1)%5 & (n2+1)%5, if any of these three result in zero then our whole result is 0. So, if n = 5, 10, .. 5k then n mod 5 = 0 hence f(n) mod 5 = 0. if n = 4, 9, .., (5k-1) then (n+1) mod 5 = 0 hence f(n) mod 5 = 0. if n = 3, 8, 13..., (5k-2) f(n) mod 5 = (3 * 4 * 10) mod 5 = 0 if n = 2, 7, 12..., (5k-3) f(n) mod 5 = (2 * 3 * 5) mod 5 = 0. if n = 1, 6, 11..., (5k-4) f(n) mod 5 = (1 * 2 * 2) mod 5 = 4.
After above analysis we can see that if n is of form 5k+1 or say 5k-4 then f(n) mod 5 = 4, other wise f(n) = 0.
I.E. if(n%5 == 1 ) result = 4,
else result = 0.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find the value of f(n) / f(r) * f(n-r)
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- 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 N from the value of N!
- Find x and y satisfying ax + by = n
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find larger of x^y and y^x
- Find N values of X1, X2, ... Xn such that X1 < X2 < ... < XN and sin(X1) < sin(X2) < ... < sin(XN)
- Find (a^b)%m where 'a' is very large
- Find minimum x such that (x % k) * (x / k) == n | Set-2
- Find minimum x such that (x % k) * (x / k) == n
- Find the value of ln(N!) using Recursion
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Find maximum value of x such that n! % (k^x) = 0
- Find the other number when LCM and HCF given
- Find the sum of all multiples of 2 and 5 below N
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.
Improved By : vt_m