473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

How to find modulus of a very large number

This is about a technique to find the mod of a very large integer with a normal small integer.

I recently encountered this problem when I needed to compute the modulus of a very large number with a normal integer. I needed this 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.
(Eg: 1088263455689473669888943602 % 380)
Not even unsigned long long or long double could help; they all were of 8bytes.
I figured out that it can be done the following way.


Solution- Divide and conquer:
The entire number doesn't have to be processed in one go. A divide and conquer strategy can be used in this case. Divide the number into smaller numbers; find remainders of these smaller numbers. Add up the remainders and repeat the process. As long as the remainders are retained, the final answer won’t 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 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.

Eg: To find 27%8:
1) Let x = 17. So nBig = nBig - x = 27-17 = 10
2) mod = x % a = 17 % 8 = 1
3) nBig = nBig + mod = 10 + 1 = 11
So nBig is reduced from 27 to 11 without affecting the answer which is 3.


Finding mod of large numbers:
Extending the same logic, the above problem can be solved as follows:-
Let input be:
nBig: String representing a 30 digit large number (Eg: "123456789033333333335555555555")
a: Integer (Eg: 320)

To find nBig % a:

Iteration 1:
1) Extract few digits from LHS of nBig.
So tmp = 1234567890 and nBig = “33333333335555555555"
2) Find mod of tmp.
mod = tmp % a = 210
3) Prefix mod to nBig.
nBig = "21033333333335555555555"

Now repeating above steps:
Iteration 2:
1) tmp = 2103333333 and nBig = "3335555555555"
2) mod = tmp % a = 213
3) nBig = "2133335555555555"

Iteration 3:
1) tmp = 2133335555 and nBig = "555555"
2) mod = tmp % a = 195
3) nBig = "195555555"

Iteration 4:
1) tmp = 195555555 and nBig = ""
2) mod = tmp % a = 35

Answer = 35


Pseudo code:
To find nBig % a:
Let d = the number of digits that can be processed at a time.

Repeat while val( nBig ) >= a
1. tmpStr := d digits from the LHS of nBig.
2. nBig := Remaining portion of nBig
3. tmpNum := toInteger( tmpStr )
4. tmpNum := tmpNum % a
5. tmpStr := toString( tmpNum )
6. nBig := tmpStr + nBig

Selecting d:
d should satisfy following constraint
max no. of digits in Mod a < d <= max. digits in tmpNum - max no. of digits in Mod a
(For eg: If a = 512 (Mod ranges 0..511), and tmpNum can store max 16 digits, then
3 < d <= 16-3)
(For eg: If a = 100 (Mod ranges 0..99), and tmpNum can store max 16 digits, then
2 < d <= 16-2)
Apr 16 '08 #1
0 22396

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mike | last post by:
My users have to select an value from a fixed selection of values. The obvious choice of control for such a requirement is to use a <select> (i.e. a combo box). My problem is that sometimes,...
3
by: Will McGugan | last post by:
Hi, Is there a simple way of replacing a large number of substrings in a string? I was hoping that str.replace could take a dictionary and use it to replace the occurrences of the keys with the...
5
by: David | last post by:
Hi all: I am processing a 3D bitmaps(essentially ~1024 2D bitmaps with a size of 1MB each). If I want read large amount of radom data from this series, how could I buffer the file to get...
3
by: vanvee | last post by:
Hi I have an application for my company's HR department where we store resumes for candidates we receive. I have an application that uses VB.Net and ADO.Net and data bindings (through code) to...
6
by: Alex | last post by:
Hi... I have a stored procedure that takes in a large number of parameters (around 30) and returns (as output parameters) another 10 or so. At the moment, each parameter is declared, defined...
2
by: =?Utf-8?B?UHJpeWE=?= | last post by:
Hi, I'm faced with a classic problem of how to update a large number of records from a web page. I;m trying to build an interface that will display recordset in the order of 3000 rows and allow...
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
3
by: Michel Esber | last post by:
Hello, Environment: DB2 LUW v8 FP15 / Linux I have a table with 50+ Million rows. The table structure is basically (ID - Timestamp). I have two main applications - one inserting rows, and the...
12
by: fermineutron | last post by:
I am trying to write a function which will convert a large ascii number, say 100 ascii digits, to its binary representation. It seems that evey algorithm I am trying to think of is backwards. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.