The Wayback Machine - https://web.archive.org/web/20240627104136/https://www.geeksforgeeks.org/remainder-7-large-numbers/
Open In App

Remainder with 7 for large numbers

Last Updated : 05 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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
Recommended Practice

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” 

  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 = 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++




// C++ program to find remainder of a large
// number when divided by 7.
#include<iostream>
using namespace std;
 
/* Function which returns 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;
}


Java




// Java program to find remainder of a large
// number when divided by 7.
 
class GFG
{
    // Function which return Remainder
    // after dividingthe number by 7
    static 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;
     
        // Initialize result
        int result = 0;
     
        // Traverse num from end
        for (int i = num.length() - 1; i >= 0; i--)
        {
            /* Find current digit of nun */
            int digit = num.charAt(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 code
    public static void main (String[] args)
    {
        String str = "12345";
        System.out.print("Remainder with 7 is "
                          +remainderWith7(str));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python3 program to find remainder of
# a large number when divided by 7.
 
# Function which return Remainder
# after dividing the number by 7
def remainderWith7(num):
     
    # This series is used to
    # find remainder with 7
    series = [1, 3, 2, -1, -3, -2];
     
    # Index of next element
    # in series
    series_index = 0;
     
    # Initialize result
    result = 0;
     
    # Traverse num from end
    for i in range((len(num) - 1), -1, -1):
         
        # Find current digit of num
        digit = ord(num[i]) - 48;
         
        # 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 Code
str = "12345";
print("Remainder with 7 is",
       remainderWith7(str));
 
# This code is contributed by mits


C#




// C# program to find the remainder of
// a large number when divided by 7.
using System;
 
class GFG
{
    // Function which return Remainder
    // after dividingthe number by 7
    static 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;
     
        // Initialize result
        int result = 0;
     
        // Traverse num from end
        for (int i = num.Length - 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 code
    public static void Main ()
    {
        String str = "12345";
        Console.Write("Remainder with 7 is " +
                         remainderWith7(str));
    }
}
 
// This code is contributed by nitin mittal.


PHP




<?php
// PHP program to find remainder of
// a large number when divided by 7.
 
/* Function which return Remainder
   after dividing the number by 7 */
function remainderWith7($num)
{
     
    // This series is used to
    // find remainder with 7
    $series = array(1, 3, 2, -1, -3, -2);
 
    // Index of next element
    // in series
    $series_index = 0;
 
    // Initialize result
    $result = 0;
 
    // Traverse num from end
    for($i = strlen($num) - 1; $i >= 0; $i--)
    {
         
        // Find current digit of num
        $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 Code
{
    $str = "12345";
    echo "Remainder with 7 is ",
         (remainderWith7($str));
    return 0;
}
 
// This code is contributed by nitin mittal.
?>


Javascript




<script>
 
// Javascript program to find remainder of a large
// number when divided by 7.
 
/* Function which returns Remainder after dividing
the number by 7 */
function remainderWith7(num)
{
 
    // This series is used to find remainder with 7
    series = [1, 3, 2, -1, -3, -2]
 
    // Index of next element in series
    var series_index = 0;
 
    var result = 0; // Initialize result
 
    // Traverse num from end
    for (var i = num.length - 1; i >= 0; i--)
    {
     
        /* Find current digit of nun */
        var 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 */
var str = "12345";
document.write("Remainder with 7 is " + remainderWith7(str));
 
// This code is contributed by oob2000.
 
</script>


Output

Remainder with 7 is 4

Time complexity : O(n) 
Auxiliary Space : O(1)



Previous Article
Next Article

Similar Reads

Program to find remainder when large number is divided by r
Given a number N, the task is to find the remainder when N is divided by R (a two-digit Number). The input of the Number may be very large. Examples: Input: N = 13589234356546756, R = 13 Output: 11 Input: N = 3435346456547566345436457867978, R = 17 Output: 13Get the digit of N one by one from left to right.For each digit, combine with next digit if
9 min read
Find N % 4 (Remainder with 4) for a large value of N
Given a string str representing a large integer, the task is to find the result of N % 4.Examples: Input: N = 81 Output: 1Input: N = 46234624362346435768440 Output: 0 Recommended: Please try your approach on {IDE} first, before moving on to the solution.Approach: The remainder of division by 4 is dependent on only the last 2 digits of a number, so
6 min read
Program to find remainder when large number is divided by 11
Given a number n, the task is to find the remainder when n is divided by 11. The input of number may be very large. Examples: Input : str = 13589234356546756Output : 6Input : str = 3435346456547566345436457867978Output : 4Since the given number can be very large, we can not use n % 11. There are some steps that needs to be used to find remainder: 1
6 min read
Multiply large integers under large modulo
Given an integer a, b, m. Find (a * b ) mod m, where a, b may be large and their direct multiplication may cause overflow. However, they are smaller than half of the maximum allowed long long int value. Examples: Input: a = 426, b = 964, m = 235Output: 119Explanation: (426 * 964) % 235 = 410664 % 235 = 119 Input: a = 10123465234878998, b = 65746311
7 min read
Largest number by which given 3 numbers should be divided such that they leaves same remainder
Given three numbers, our task is to find the largest number by which when given 3 numbers are divided leads to same remainder. It may be assumed that all given numbers are given in increasing order. Examples: Input : a = 62, b = 132, c = 237 Output : 35 35 leads to same remainder 27 when divides 62, 132 and 237. Input : a = 74, b = 272, c = 584 Out
9 min read
Find numbers that divide X and Y to produce the same remainder
Given two integers X and Y, the task is to find and print the numbers that divide X and Y to produce the same remainder.Examples: Input: X = 1, Y = 5 Output: 1, 2, 4 Explanation: Let the number be M. It can be any value in the range [1, 5]: If M = 1, 1 % 1 = 0 and 5 % 1 = 0 If M = 2, 1 % 2 = 1 and 5 % 2 = 1 If M = 3, 1 % 3 = 1 and 5 % 3 = 2 If M =
9 min read
Count of pairs from first N natural numbers with remainder at least K
Given two positive integers N and K, the task is to find the number of pairs (a, b) over the range [1, N] such that a%b is at least K. Examples: Input: N = 5, K = 2Output: 7Explanation: Following are the all possible pairs satisfying the given criteria: (2, 3): The value of 2%3 = 2(&gt;= K).(5, 3): The value of 5%3 = 2(&gt;= K).(2, 4): The value of
6 min read
Program to find Quotient And Remainder in Java
The remainder is the integer left over after dividing one integer by another. The quotient is the quantity produced by the division of two numbers. For example, (7/2) = 3 In the above expression 7 is divided by 2, so the quotient is 3 and the remainder is 1. Approach: Divide the dividend by the divisor using / operator. Both dividend and divisor ca
2 min read
Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
Given three integer N, K and X, the task is to create an array of length N such that sum of all its K-length subarrays modulo N is X.Examples: Input: N = 6, K = 3, X = 3 Output: 9 6 6 9 6 6 Explanation: All subarrays of length 3 and their respective sum%N values are as follows: [9, 6, 6] Sum = 21 % 6 = 3 [6, 6, 9] sum = 21 % 6 = 3 [6, 9, 6] sum = 2
4 min read
C Program to Compute Quotient and Remainder
Given two numbers A and B. The task is to write a program to find the quotient and remainder of these two numbers when A is divided by B. Examples: Input: A = 2, B = 6 Output: Quotient = 0, Remainder = 2 Input: A = 17, B = 5 Output: Quotient = 3, Remainder = 21. Using Division and Modulo Operator In the below program, to find the quotient and remai
7 min read
three90RightbarBannerImg