Given a large number as a string, find the remainder of number when divided by 7.
Examples :
Input : num = 1234
Output : 2
Input : num = 1232
Output : 0
Input : num = 12345
Output : 4
Simple Approach is to convert a string into number and perform the mod operation. But this approach will not work for long strings.
There exists an approach that works for large numbers also.
Below are the steps. Let the given number be “num”
- We use a series 1, 3, 2, -1, -3, -2 to find the remainder (Intuition behind the series is discussed below).
- Initialize the result as 0.
- Traverse num from end and above series from beginning. For every traversed digit, multiply it with next digit of series, and add the multiplication to result.
- Keep repeating step 3 while there are more digits to process. If there are more than 6(number of terms in series) digits, then start traversing the series from beginning.
- After each step, we keep doing result = result % 7 to make sure that result remains less than 7.
Illustration :
let us take above Example where number is 12345.
We reverse the number from end and series from
the beginning and keep adding multiplication to
the result.
(12345 % 7) = (5*1 + 4*3 + 3*2 + 2*(-1) + 1*(-3)) % 7
= (5 + 12 + 6 - 2 - 3)%7
= (18) % 7
= 4
hence 4 will be the remainder when we divide
the number 12345 by 7.
How does this series formula work?
Below is the intuition behind the series
1 % 7 = 1
10 % 7 = 3
100 % 7 = 2
1000 % 7 = 6 = (-1) % 7
10000 % 7 = 4 = (-3) % 7
100000 % 7 = 5 = (-2) % 7
The series repeats itself for larger powers
1000000 % 7 = 1
10000000 % 7 = 3
..............
...............
The above property of modular division with 7 and
associative properties of modular arithmetic are
the basis of the approach used here.
Implementation:
C++
#include<iostream>
using namespace std;
int remainderWith7(string num)
{
int series[] = {1, 3, 2, -1, -3, -2};
int series_index = 0;
int result = 0;
for ( int i=num.size()-1; i>=0; i--)
{
int digit = num[i] - '0' ;
result += digit * series[series_index];
series_index = (series_index + 1) % 6;
result %= 7;
}
if (result < 0)
result = (result + 7) % 7;
return result;
}
int main()
{
string str = "12345" ;
cout << "Remainder with 7 is "
<< remainderWith7(str);
return 0;
}
|
Java
class GFG
{
static int remainderWith7(String num)
{
int series[] = { 1 , 3 , 2 , - 1 , - 3 , - 2 };
int series_index = 0 ;
int result = 0 ;
for ( int i = num.length() - 1 ; i >= 0 ; i--)
{
int digit = num.charAt(i) - '0' ;
result += digit * series[series_index];
series_index = (series_index + 1 ) % 6 ;
result %= 7 ;
}
if (result < 0 )
result = (result + 7 ) % 7 ;
return result;
}
public static void main (String[] args)
{
String str = "12345" ;
System.out.print( "Remainder with 7 is "
+remainderWith7(str));
}
}
|
Python3
def remainderWith7(num):
series = [ 1 , 3 , 2 , - 1 , - 3 , - 2 ];
series_index = 0 ;
result = 0 ;
for i in range (( len (num) - 1 ), - 1 , - 1 ):
digit = ord (num[i]) - 48 ;
result + = digit * series[series_index];
series_index = (series_index + 1 ) % 6 ;
result % = 7 ;
if (result < 0 ):
result = (result + 7 ) % 7 ;
return result;
str = "12345" ;
print ( "Remainder with 7 is" ,
remainderWith7( str ));
|
C#
using System;
class GFG
{
static int remainderWith7(String num)
{
int []series = {1, 3, 2, -1, -3, -2};
int series_index = 0;
int result = 0;
for ( int i = num.Length - 1; i >= 0; i--)
{
int digit = num[i] - '0' ;
result += digit * series[series_index];
series_index = (series_index + 1) % 6;
result %= 7;
}
if (result < 0)
result = (result + 7) % 7;
return result;
}
public static void Main ()
{
String str = "12345" ;
Console.Write( "Remainder with 7 is " +
remainderWith7(str));
}
}
|
PHP
<?php
function remainderWith7( $num )
{
$series = array (1, 3, 2, -1, -3, -2);
$series_index = 0;
$result = 0;
for ( $i = strlen ( $num ) - 1; $i >= 0; $i --)
{
$digit = $num [ $i ] - '0' ;
$result += $digit *
$series [ $series_index ];
$series_index = ( $series_index + 1) % 6;
$result %= 7;
}
if ( $result < 0)
$result = ( $result + 7) % 7;
return $result ;
}
{
$str = "12345" ;
echo "Remainder with 7 is " ,
(remainderWith7( $str ));
return 0;
}
?>
|
Javascript
<script>
function remainderWith7(num)
{
series = [1, 3, 2, -1, -3, -2]
var series_index = 0;
var result = 0;
for ( var i = num.length - 1; i >= 0; i--)
{
var digit = num[i] - '0' ;
result += digit * series[series_index];
series_index = (series_index + 1) % 6;
result %= 7;
}
if (result < 0)
result = (result + 7) % 7;
return result;
}
var str = "12345" ;
document.write( "Remainder with 7 is " + remainderWith7(str));
</script>
|
Output
Remainder with 7 is 4
Time complexity : O(n)
Auxiliary Space : O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!