Modular Multiplication: How to Efficiently Calculate (a * b) mod c in C/C++?

  • Thread starter Thread starter chesschi
  • Start date Start date
  • Tags Tags
    Multiplication
AI Thread Summary
To efficiently calculate (a * b) mod c for three 64-bit numbers in C/C++, one approach involves breaking down the multiplication into smaller components to avoid overflow. The formula a * b can be expressed as x1y1 * 2^64 + (x0y1 + x1y0) * 2^32 + x0y0, where a and b are split into high and low parts. The challenge lies in managing the potential overflow when a and b exceed 32 bits. Using modular arithmetic during the multiplication process can help keep the intermediate results within bounds. Implementing this method can yield an efficient solution without relying on libraries like BigInt or GMP.
chesschi
Messages
3
Reaction score
0
I want to write a C/C++ program and encounter a problem.If I have three 64 bit numbers and need to manipulate
"a * b mod c"

Is there any well-known, efficient and simple algorithm to implement it?

I only know it can be calculated sth. like this..
__int64 a, b, c
a = x1 * 2^32 + x0
b = y1 * 2^32 + y0

then a * b = x1y1 * 2^64 + (x0y1 + x1y0) * 2^32 + x0y0

but I don't know what to do in the next step

Note that this cannot be calculated using some classes like BigInt, gmp

Thank you very much!
 
Mathematics news on Phys.org
chesschi said:
I want to write a C/C++ program and encounter a problem.If I have three 64 bit numbers and need to manipulate
"a * b mod c"

Is there any well-known, efficient and simple algorithm to implement it?

I only know it can be calculated sth. like this..
__int64 a, b, c
a = x1 * 2^32 + x0
b = y1 * 2^32 + y0
C, C++, C#, and other languages that are based on C have a modulus operator, %

C:
unsigned long long a, b, c;
// Assign values to a, b, and c
x =  a * b % c;
One thing to be concerned about is that if a and b are larger than 32 bits, their product won't fit in 64 bits, and you'll get overflow.
chesschi said:
then a * b = x1y1 * 2^64 + (x0y1 + x1y0) * 2^32 + x0y0

but I don't know what to do in the next step

Note that this cannot be calculated using some classes like BigInt, gmp

Thank you very much!
 
Thread 'Video on imaginary numbers and some queries'
Hi, I was watching the following video. I found some points confusing. Could you please help me to understand the gaps? Thanks, in advance! Question 1: Around 4:22, the video says the following. So for those mathematicians, negative numbers didn't exist. You could subtract, that is find the difference between two positive quantities, but you couldn't have a negative answer or negative coefficients. Mathematicians were so averse to negative numbers that there was no single quadratic...
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Thread 'Unit Circle Double Angle Derivations'
Here I made a terrible mistake of assuming this to be an equilateral triangle and set 2sinx=1 => x=pi/6. Although this did derive the double angle formulas it also led into a terrible mess trying to find all the combinations of sides. I must have been tired and just assumed 6x=180 and 2sinx=1. By that time, I was so mindset that I nearly scolded a person for even saying 90-x. I wonder if this is a case of biased observation that seeks to dis credit me like Jesus of Nazareth since in reality...

Similar threads

Replies
14
Views
7K
Replies
14
Views
4K
Replies
11
Views
3K
Replies
13
Views
3K
Back
Top