Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
Last Updated :
09 Jun, 2022
Given a very large number N. The task is to find (1n + 2n + 3n + 4n) mod 5.
Examples:
Input: N = 4
Output: 4
(1 + 16 + 81 + 256) % 5 = 354 % 5 = 4
Input: N = 7823462937826332873467731
Output: 0
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:
C++
#include <bits/stdc++.h>
using namespace std;
int A_mod_B(string N, int a)
{
int len = N.size();
int ans = 0;
for ( int i = 0; i < len; i++)
ans = (ans * 10 + ( int )N[i] - '0' ) % a;
return ans % a;
}
int findMod(string N)
{
int mod = A_mod_B(N, 4);
int ans = (1 + pow (2, mod) + pow (3, mod)
+ pow (4, mod));
return (ans % 5);
}
int main()
{
string N = "4" ;
cout << findMod(N);
return 0;
}
|
Java
class GFG
{
static int A_mod_B(String N, int a)
{
int len = N.length();
int ans = 0 ;
for ( int i = 0 ; i < len; i++)
ans = (ans * 10 + ( int )N.charAt(i) - '0' ) % a;
return ans % a;
}
static int findMod(String N)
{
int mod = A_mod_B(N, 4 );
int ans = ( 1 + ( int )Math.pow( 2 , mod) +
( int )Math.pow( 3 , mod) +
( int )Math.pow( 4 , mod));
return (ans % 5 );
}
public static void main(String args[])
{
String N = "4" ;
System.out.println(findMod(N));
}
}
|
Python3
def A_mod_B(N, a):
Len = len (N)
ans = 0
for i in range ( Len ):
ans = (ans * 10 + int (N[i])) % a
return ans % a
def findMod(N):
mod = A_mod_B(N, 4 )
ans = ( 1 + pow ( 2 , mod) +
pow ( 3 , mod) + pow ( 4 , mod))
return ans % 5
N = "4"
print (findMod(N))
|
C#
using System;
class GFG
{
static int A_mod_B( string N, int a)
{
int len = N.Length;
int ans = 0;
for ( int i = 0; i < len; i++)
ans = (ans * 10 + ( int )N[i] - '0' ) % a;
return ans % a;
}
static int findMod( string N)
{
int mod = A_mod_B(N, 4);
int ans = (1 + ( int )Math.Pow(2, mod) +
( int )Math.Pow(3, mod) +
( int )Math.Pow(4, mod));
return (ans % 5);
}
public static void Main()
{
string N = "4" ;
Console.WriteLine(findMod(N));
}
}
|
PHP
<?php
function A_mod_B( $N , $a )
{
$len = strlen ( $N );
$ans = 0;
for ( $i = 0; $i < $len ; $i ++)
$ans = ( $ans * 10 +
(int) $N [ $i ] - '0' ) % $a ;
return $ans % $a ;
}
function findMod( $N )
{
$mod = A_mod_B( $N , 4);
$ans = (1 + pow(2, $mod ) +
pow(3, $mod ) + pow(4, $mod ));
return ( $ans % 5);
}
$N = "4" ;
echo findMod( $N );
?>
|
Javascript
<script>
function A_mod_B(N, a)
{
var len = N.length;
var ans = 0;
for ( var i = 0; i < len; i++)
ans = (ans * 10 + parseInt(N.charAt(i) - '0' )) % a;
return ans % a;
}
function findMod(N)
{
var mod = A_mod_B(N, 4);
var ans = (1 + parseInt(Math.pow(2, mod) +
Math.pow(3, mod) +
Math.pow(4, mod)));
return (ans % 5);
}
var N = "4" ;
document.write(findMod(N));
</script>
|
Time Complexity: O(|N|), where |N| is the length of the string.
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...