Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n - GeeksforGeeks

07 Apr, 2021
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. Examples :  
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. 
C++
Java
Python3
C#
PHP
Javascript
// finding the value of f(n) mod 5 for given n.#include <bits/stdc++.h>using namespace std; // function for f(n) mod 5int fnMod(int n){    // if n % 5 == 1 return 4    if (n % 5 == 1)        return 4;     // else return 0    else        return 0;} // driver programint main(){    int n = 10;    cout << fnMod(n) << endl;    n = 11;    cout << fnMod(n) << endl;    return 0;}
// Java code to finding the value// of f(n) mod 5 for given n.import java.io.*; class GFG{    // function for f(n) mod 5    static int fnMod(int n)    {        // if n % 5 == 1 return 4        if (n % 5 == 1)            return 4;             // else return 0        else            return 0;    }         // Driver program    public static void main (String[] args)    {        int n = 10;        System.out.println(fnMod(n));        n = 11;        System.out.println(fnMod(n));     }} // This code is contributed by vt_m.
# Python3 program to find the value# of f(n) mod 5 for given n. # Function for f(n) mod 5def fnMod(n):     # if n % 5 == 1 return 4    if (n % 5 == 1):        return 4     # else return 0    else:        return 0 # Driver Coden = 10print(fnMod(n)) n = 11print(fnMod(n)) # This code is contributed by Smitha Dinesh Semwal
// Code for finding the value// of f(n) mod 5 for given n.using System; class GFG {    // function for f(n) mod 5    static int fnMod(int n)    {        // if n % 5 == 1 return 4        if (n % 5 == 1)            return 4;         // else return 0        else            return 0;    }     // Driver program    public static void Main()    {        int n = 10;        Console.WriteLine(fnMod(n));        n = 11;        Console.WriteLine(fnMod(n));    }} // This code is contributed by vt_m.
<?php// PHP program for finding the// value of f(n) mod 5 for given n. // function for f(n) mod 5function fnMod($n){    // if n % 5 == 1 return 4    if ($n % 5 == 1)        return 4;     // else return 0    else        return 0;} // Driver Code$n = 10;echo fnMod($n),"\n";$n = 11;echo fnMod($n) ; // This code is contributed by anuj_67.?>
<script>// JavaScript program to finding the value// of f(n) mod 5 for given n. // function for f(n) mod 5    function fnMod(n)    {             // if n % 5 == 1 return 4        if (n % 5 == 1)            return 4;               // else return 0        else            return 0;    }  // Driver Code        let n = 10;        document.write(fnMod(n) + "<br/>");        n = 11;        document.write(fnMod(n) + "<br/>");                        // This code is contributed by chinmoy1997pal.      </script>
Output :  
0
4
 
vt_m
chinmoy1997pal
Modular Arithmetic
Mathematical
Mathematical
Modular Arithmetic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Operators in C / C++
Euclidean algorithms (Basic and Extended)
Program for Decimal to Binary Conversion
Algorithm to solve Rubik's Cube
Minimum number of jumps to reach end