Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Any
Expertise: Beginner
Sep 2, 2008

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

Sameer Palande
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap