Finding Modulus of a Very Large Number with a Normal Number

Finding Modulus of a Very Large Number with a Normal Number

I recently encountered this problem in a C++ program running on a 32-bit UNIX platform. The problem was that the number was 28 digits long and no native datatype in C++ could store a number of that size?not even unsigned long long or long double could help; they were only eight bytes long.

A divide-and-conquer strategy can be used in this case?the entire number doesn’t have to be processed in one go. Divide the number into smaller numbers, then find the remainders of these smaller numbers. Add up the remainders and repeat the process. As long as the remainders are retained, the final answer wont change.

Suppose you need to find nBig % a:

  1. Take a manageable portion (say, x) of the input large number (nBig) and subtract it from nBig (nBig = nBig – x).
  2. Compute the mod of x (mod = x % a).
  3. Add the mod to nBig (nBig = nBig + mod). This way, the number nBig can be reduced without affecting the actual answer.

This first example uses small numbers, so it’s easy to follow the logic. To find 27 % 8:

  1. Let x = 17. Thus, nBig = nBig – x = 27-17 = 10.
  2. mod = x % a = 17 % 8 = 1
  3. nBig = nBig + mod = 10 + 1 = 11
  4. Thus, nBig is reduced from 27 to 11 without affecting the answer, which is 3.

But what about finding the mod of large numbers? Extend the same logic, and the above problem can be solved as follows:

  1. Let the input be:
    • nBig: This is a string representing a 30 digit large number (for example, 123456789033333333335555555555)
    • a: Integer (for example, 320)
  2. To find nBig % a:
    1. Iteration 1:
      1. Extract few digits from the LHS of nBig. Thus, tmp = 1234567890 and nBig = 33333333335555555555.
      2. Find mod of tmp:
            mod = tmp % a = 210
      3. Prefix mod to nBig:
            nBig = 21033333333335555555555
    2. Iteration 2:
      1. tmp = 2103333333 and nBig = 3335555555555
      2. mod = tmp % a = 213
      3. nBig = 2133335555555555
    3. Iteration 3:
      1. tmp = 2133335555 and nBig = 555555
      2. mod = tmp % a = 195
      3. nBig = 195555555
    4. Iteration 4:
      1. tmp = 195555555 and nBig = “”
      2. mod = tmp % a = 35
    5. The Answer = 35.

What follows is the pseudo code for finding nBig % a. First, let d = the number of digits that can be processed at a time. Then, repeat while val( nBig ) &gt= a.

  1. tmpStr: Equals d digits from the LHS of nBig.
  2. nBig: Equals the remaining portion of nBig.
  3. tmpNum: Equals toInteger(tmpStr).
  4. tmpNum: Equals tmpNum % a.
  5. tmpStr: Equals toString(tmpNum).
  6. nBig: Equals tmpStr + nBig.

To select d, make sure d satisfies the following constraint: The maximum number of digits in Mod a &lt d < should equal the maximum tmpNum minus the maximum number of digits in Mod.

For example, suppose you have a = 512 (Mod ranges 0..511), and your tmpNum can store a maximum of 16 digits. Then, you’d have:

3 < d <= 16-3)

In another example, suppose a = 100 (Mod ranges 0..99), and tmpNum can store a maximum of 16 digits. In that case, you'd have:

2 < d <= 16-2)

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as