You are given an positive integer n. You have to find the value of (1^{n} +2^{n} + 3^{n} + 4^{n} ) mod 5.

**Note :** Value of n may be very large of order 10^{15}.

Examples:

Input : n = 4 Output : 4 Explanation : (1^{4}+ 2^{4}+ 3^{4}+ 4^{4})mod 5 = (1+16+81+256)mod 5 = 354 mod 5 = 4 Input : n = 2 Output : 0 Explanation : (1^{2}+ 2^{2}+ 3^{2}+ 4^{2})mod 5 = (1+4+9+16)mod 5 = 30 mod 5 = 0

**Basic Approach :** If you will solve this question with a very basic approach of finding value of (1^{n} +2^{n} + 3^{n} + 4^{n} ) and then finding its modulo value for 5, you will certainly get your answer but for the larger value of n we must got wrong answer as you will be unable to store value of (1^{n} +2^{n} + 3^{n} + 4^{n} ) properly.

**Better and Proper Approach : ** Before proceeding to solution lets go through some of periodical properties of power of 2, 3 & 4.

- f(n) = 2
^{n}is periodical for n = 4 in terms of last digit. i.e. last digit of 2^{n}always repeat for next 4th value of n. (ex: 2, 4, 8, 16, 32, 64…) - f(n) = 3
^{n}is periodical for n = 4 in terms of last digit. i.e. last digit of 3^{n}always repeat for next 4th value of n.(ex: 3, 9, 27, 81, 243, 729…) - f(n) = 4
^{n}is periodical for n = 2 in terms of last digit. i.e. last digit of 4^{n}always repeat for next 2nd value of n.(ex: 4, 16, 64, 256..) - 1
^{n}is going to be 1 always, independent of n.

So, If we will have a close look for periodicity of f(n) = (1^{n} +2^{n} + 3^{n} + 4^{n} ) we will get that its periodicity is also 4 and its last digits occurs as :

- for n = 1, f(n) = 10
- for n = 2, f(n) = 30
- for n = 3, f(n) = 100
- for n = 4, f(n) = 354
- for n = 5, f(n) = 1300

Observing above periodicity we can see that if (n%4==0) result of f(n)%5 is going to be 4 other wise result = 0. So, rather than calculating actual value of f(n) and then obtaining its value with mod 5 we can easily get result only be examine value of n.

## C++

`// Program to find value of f(n)%5 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function for obtaining remainder ` `int` `fnMod(` `int` `n) ` `{ ` ` ` `// calculate res based on value of n ` ` ` `return` `(n % 4) ? 0 : 4; ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `int` `n = 43; ` ` ` `cout << fnMod(n) << endl; ` ` ` `n = 44; ` ` ` `cout << fnMod(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Program to find value of f(n)% 5 ` ` ` `class` `GFG ` `{ ` ` ` `// function for obtaining remainder ` ` ` `static` `int` `fnMod(` `int` `n) ` ` ` `{ ` ` ` `// calculate res based on value of n ` ` ` `return` `(n % ` `4` `!= ` `0` `) ? ` `0` `: ` `4` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `43` `; ` ` ` `System.out.println(fnMod(n)); ` ` ` `n = ` `44` `; ` ` ` `System.out.print(fnMod(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python

`# program to find f(n) mod 5 ` `def` `fnMod (n): ` ` ` `res ` `=` `4` `if` `(n ` `%` `4` `=` `=` `0` `) ` `else` `0` ` ` `return` `res ` ` ` `# driver section ` `n ` `=` `43` `print` `(fnMod(n)) ` `n ` `=` `44` `print` `(fnMod(n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find value of f(n) % 5 ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function for obtaining remainder ` ` ` `static` `int` `fnMod(` `int` `n) ` ` ` `{ ` ` ` `// calculate res based on value of n ` ` ` `return` `(n % 4 != 0) ? 0 : 4; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 43; ` ` ` `Console.WriteLine(fnMod(n)); ` ` ` `n = 44; ` ` ` `Console.Write(fnMod(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find value of f(n)%5 ` ` ` `// function for obtaining remainder ` `function` `fnMod(` `$n` `) ` `{ ` ` ` ` ` `// calculate res based ` ` ` `// on value of n ` ` ` `return` `(` `$n` `% 4) ? 0 : 4; ` `} ` ` ` `// Driver Code ` `{ ` ` ` `$n` `= 43; ` ` ` `echo` `fnMod(` `$n` `),` `"\n"` `; ` ` ` `$n` `= 44; ` ` ` `echo` `fnMod(` `$n` `); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0 4

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.

## Recommended Posts:

- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Find the value of f(n) / f(r) * f(n-r)
- Find K such that |A - K| = |B - K|
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find 2^(2^A) % B
- Find N from the value of N!
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9
- Program to find value of 1^k + 2^k + 3^k + ... + n^k
- Find the value of N XOR'ed to itself K times
- Find the ln(X) and log
_{10}X with the help of expansion - Find the sum of all multiples of 2 and 5 below N
- Find the sum of series 3, 7, 13, 21, 31....
- Find x and y satisfying ax + by = n
- Given two numbers a and b find all x such that a % x = b
- Find any pair with given GCD and LCM
- Find (a^b)%m where 'a' is very large

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.