Remainder with 7 for large numbers

3.8

Given a large number as string, find 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 string into number and perform mod operation. But this approach will not work for long strings.

The exist an approach that works for large numbers also. Below are the steps.

Let the given number be “num”

  1. We use a series 1, 3, 2, -1, -3, -2 to find the remainder (Intuition behind the series is discussed below).
  2. Initialize the result as 0.
  3. 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.
  4. 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.
  5. 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 = 3 = (-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 user here.

Implementation:

// C++ program to find remainder of a large
// number when divided by 7.
#include<iostream>
using namespace std;

/* Function which return Remainder after dividing
   the number by 7 */
int remainderWith7(string num)
{
    // This series is used to find remainder with 7
    int series[] = {1, 3, 2, -1, -3, -2};

    // Index of next element in series
    int series_index = 0;

    int result = 0;  // Initialize result

    // Traverse num from end
    for (int i=num.size()-1; i>=0; i--)
    {
        /* Find current digit of nun */
        int digit = num[i] - '0';

        // Add next term to result
        result += digit * series[series_index];

        // Move to next term in series
        series_index = (series_index + 1) % 6;

        // Make sure that result never goes beyond 7.
        result %= 7;
    }

    // Make sure that remainder is positive
    if (result < 0)
      result = (result + 7) % 7;

    return result;
}

/* Driver program */
int main()
{
    string str = "12345";
    cout << "Remainder with 7 is "
         << remainderWith7(str);
    return 0;
}

Output:

  4

This article is contributed by Kuldeep Singh . 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



3.8 Average Difficulty : 3.8/5.0
Based on 6 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.