Modular arithmetic

1. An introduction

When we divide two integers we will have an equation that looks like the

following:

A/B = Q remainder R

A  is the dividend

B is the divisor

Q is the quotient

R is the remainder

Sometimes, we are only interested in what the remainder is when we divide AA by BB.
For these cases there is an operator called the modulo operator (abbreviated as mod).

Using the same AABBQQ, and RR as above, we would have: A \text{ mod } B = RA mod B=R.We would say this as AA modulo BB is congruent to RR. Where BB is referred to as the modulus.

For example:

13/5 = 2 remainder 3
13 mod 5 = 3
If we have  A \text{ mod } BA mod B  and we increase  AA  by a  multiple of  B , we will end up in
the same result:

A mod B=(A+KB) mod B for any integer K.


2. Congruence Modulo

You may see an expression like:

AB(mod C)equivalent

This says that AA is congruent to BB modulo CC.

Let's imagine we were calculating mod 5 for all of the integers:

Suppose we labelled 5  slices  0, 1, 2, 3, 4. Then, for each of the integers, we

put it into a slice that matched the value of the integer mod 5.Think of these slices as buckets, which hold a set of numbers. For example, 26would go in the slicelabeled 1,  because 26 mod 5=1.It would be useful to have a way of expressing that numbers belonged in the same slice. (Notice 26 is in the same slice as 1, 6, 11, 16, 21 in above example).

A common way of expressing that two values are in the same slice, is to say they are in the same equivalence class.The way we express this mathematically for mod C is: A \equiv B \ (\text{mod } C)AB (mod C).

Examining the expression closer:

1. is the symbol for congruence, which means the values AA and BB are in
the same equivalence class.

2.(mod C) tells us what operation we applied to AA and BB.

3.when we have both of these, we call “\equiv” congruence modulo CC.

e.g. 26 \equiv 11\ (\text{mod }5)2611 (mod 5)


we can make a key observation:

The values in each of the slices are equal to the label on the slice plus or minus some multiple of  C .

This means the difference between any two values in a slice is some multipleof  C .

Before proceeding it’s important to remember the following statements are equivalent:

AB (mod C)

A mod CB mod C

C  (AB) The | symbol means divides, or is a factor of)

A=B+KC (where KK is some integer)


For example the following are equivalent:

1323 (mod 5)

13 mod 523 mod 5

5  (1323)(5 \ |\ -10(5  10, which is true since  5×2=10)
13=23+K5. We can satisfy this with  K=2 :
13=23+(2)×5

 Congruence Modulo is an Equivalence Relation


The Quotient Remainder Theorem
When we want to prove some properties about modular arithmetic we oftenmake use of the Quotient Remainder Theorem.It is a simple idea that comes directly from long division.

The Quotient Remainder theorem says:

Given any integer A, and a positive integer B, there exist unique integers Q
and R
 such that

A= B * Q + R where 0 ≤ R < B
We can see that this comes directly from long division. When we  divide A by B in long division, Q is the quotient and  R is the remainder . If we can write a number in this form then  A mod B = R

A = 7, B = 2 

7 = 2 * 3 + 1 
7 mod 2 = 1 

A = 8, B = 4 

8 = 4 * 2 + 0 
8 mod 4 = 0 

A = 13, B = 5 

13 = 5 * 2 + 3 
13 mod 5 = 3 

A = -16, B = 26

-16 = 26 * -1 + 10
-16 mod 26 = 10



Modular addition andsubtraction

(A + B) mod C = (A mod C + B mod C) mod C
Proof:

From the quotient remainder theorem we can write A and B as:

A = C * Q1 + R1 where 0 ≤ R1 < C and Q1 is some integer. A mod C = R1

B = C * Q2 + R2 where 0 ≤ R2 < C and Q2 is some integer. B mod C = R2

(A + B) = C * (Q1 + Q2) + R1+R2

(A + B) mod C = ( C *(Q1 + Q2) + R1 + R2) mod C = (R1 + R2) mod C

(A mod C + B mod C) mod C = (R1 + R2) mod c


A very similar proof holds for modular subtraction

(A - B) mod C = (A mod C - B mod C) mod C


Let's explore the multiplication property of modular arithmetic:

(A * B) mod C = (A mod C * B mod C) mod C


Finally, let's explore the exponentiation property:

A^B mod C = ( (A mod C)^B ) mod C
Often we want to calculate  A^B mod C  for  large values of B .

Unfortunately, A^B becomes very large for even modest sized values for B.

These huge values cause our calculators and computers to

return overflowerrors.Even if they didn't, it would take a long time 

to find the mod of these hugenumbers directly.

Suppose we want to calculate 2^90 mod 13, but we have a calculator that
can't hold any numbers larger than 2^50.

Here is a simple divide and conquer strategy:

Step 1: Split A^B into smaller parts using exponent rules

2^90 = 2^50 * 2^40

Step 2: Calculate the mod C for each part

2^50 mod 13 = 1125899906842624 mod 13 = 4

2^40 mod 13 = 1099511627776 mod 13 = 3

Step 3: Use modular multiplication properties to combine the parts

2^90 mod 13 = (2^50 * 2^40) mod 13

2^90 mod 13 = (2^50 mod 13 * 2^40 mod 13) mod 13

2^90 mod 13 = ( 4 * 3 ) mod 13

2^90 mod 13 = 12 mod 13 = 12


https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值