An Introduction to Modular Math

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

AB=Q remainder R\dfrac{A}{B} = Q \text{ remainder } R​B​​A​​=Q remainder R

AAA
is the dividend

BBB
is the divisor

QQQ
is the quotient

RRR
is the remainder

Sometimes, we are only interested in what the remainder is when we divide
AAA
by BBB.

For these cases there is an operator called the modulo operator (abbreviated as mod).

Using the same AAA,
BBB,
QQQ,
and RRR
as above, we would have: A mod B=RA \text{ mod } B = RA mod B=R

We would say this as AAA
modulo BBB
is congruent to RRR.
Where BBB
is referred to as the modulus.

For example:

13513 mod 5==2 remainder 33

Visualize modulus with clocks

Observe what happens when we increment numbers by one and then divide them by 3.

03132333435363=======0 remainder 00 remainder 10 remainder 21 remainder 01 remainder 11 remainder 22 remainder 0

The remainders start at 0 and increases by 1 each time, until the number reaches one less than the number we are dividing by. After that, the sequence
repeats.

By noticing this, we can visualize the modulo operator by using circles.

We write 0 at the top of a circle and continuing clockwise writing integers 1, 2, ... up to one less than the modulus.

For example, a clock with the 12 replaced by a 0 would be the circle for a modulus of 12.

To find the result of A mod BA \text{ mod } BA mod B
we can follow these steps:

  1. Construct this clock for size BBB
  2. Start at 0 and move around the clock AAA
    steps
  3. Wherever we land is our solution.

(If the number is positive we step clockwise, if it's negative we step
counter-clockwise.)


Examples

8 mod 4=?8 \text{ mod } 4 = ?8 mod 4=?

With a modulus of 4 we make a clock with numbers 0, 1, 2, 3.

We start at 0 and go through 8 numbers in a clockwise sequence 1, 2, 3, 0, 1, 2, 3, 0.

We ended up at 0 so 8 mod 4=0.

7 mod 2=?7 \text{ mod } 2 = ?7 mod 2=?

With a modulus of 2 we make a clock with numbers 0, 1.

We start at 0 and go through 7 numbers in a clockwise sequence 1, 0, 1, 0, 1, 0, 1.

We ended up at 1 so 7 mod 2=1.

−5 mod 3=?-5 \text{ mod } 3 = ?−5 mod 3=?

With a modulus of 3 we we make a clock with numbers 0, 1, 2.

We start at 0 and go through 5 numbers in counter-clockwise sequence (5 is
negative) 2, 1, 0, 2, 1.

We ended up at 1 so −5 mod 3=1.


Conclusion

If we have A mod BA \text{ mod } BA mod B and
we increase AAA
by a multiple of B,
we will end up in the same spot, i.e.

A mod B=(A+K⋅B) mod BA \text{ mod } B = (A + K \cdot B) \text{ mod } BA mod B=(A+K⋅B) mod B for
any integer K.

For example:

3 mod 10=313 mod 10=323 mod 10=333 mod 10=3

Notes to the Reader

mod in programming languages and calculators

Many programming languages, and calculators, have a mod operator, typically represented with the % symbol. If you calculate the result of a negative number, some languages will give you a negative result.

e.g.

-5 % 3 = -2.In a future article we will explain, why this happens, and what it means.

Congruence Modulo

You may see an expression like:

A≡B (mod C)A \equiv B\ (\text{mod } C)A≡B (mod C)

This says that AAA
is congruent to BBB
modulo CCC.
It is similar to the expressions we used here, but not quite the same.

In the next article we will explain what it means and how it is related to the expressions above.

Mod in math的更多相关文章

  1. VB6与VB.NET对照表

    VB6与VB.NET对照表 VB6.0 VB.NET AddItem Object名.AddItem Object名.Items.Add ListBox1.Items.Add ComboBox1.It ...

  2. VB6.0 和VB.NET 函数对比

    VB6.0和VB.Net的对照表 VB6.0 VB.NET AddItem Object名.AddItem Object名.Items.Add ListBox1.Items.Add ComboBox1 ...

  3. Java的数组长度无需编译指定,因为它是对象

    大家可以看从Thinking in Java中摘出来的代码理解一下,甚至.多维数组的子数组无须等长 //: MultiDimArray.java// Creating multidimensional ...

  4. VB6.0和VB.Net的函数等对照表

    VB6.0和VB.Net的对照表 VB6.0 VB.NET AddItem Object名.AddItem Object名.Items.Add ListBox1.Items.Add ComboBox1 ...

  5. 利用eval函数实现简单的计算器

    """ description : use python eval() function implement a simple calculator functions ...

  6. [洛谷P4245]【模板】任意模数NTT

    题目大意:给你两个多项式$f(x)$和$g(x)$以及一个模数$p(p\leqslant10^9)$,求$f*g\pmod p$ 题解:任意模数$NTT$,最大的数为$p^2\times\max\{n ...

  7. 子数组最小值的总和 Sum of Subarray Minimums

    2018-09-27 23:33:49 问题描述: 问题求解: 方法一.DP(MLE) 动态规划的想法应该是比较容易想到的解法了,因为非常的直观,但是本题的数据规模还是比较大的,如果直接使用动态规划, ...

  8. 动态规划-填格子问题 Domino and Tromino Tiling

    2018-09-01 22:38:19 问题描述: 问题求解: 本题如果是第一看到,应该还是非常棘手的,基本没有什么思路. 不妨先从一种简化的版本来考虑.如果仅有一种砖块,那么,填充的方式如下.

  9. SharePoint REST API - OData查询操作

    博客地址:http://blog.csdn.net/FoxDave 本篇主要讲述SharePoint REST中OData的查询操作.SharePoint REST服务支持很多OData查询字符串 ...

随机推荐

  1. WCF技术剖析之十七:消息(Message)详解(下篇)

    原文:WCF技术剖析之十七:消息(Message)详解(下篇) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话)]]< ...

  2. MYSQL获取自增主键【4种方法】

    通常我们在应用中对mysql执行了insert操作后,需要获取插入记录的自增主键.本文将介绍java环境下的4种方法获取insert后的记录主键auto_increment的值: 通过JDBC2.0提 ...

  3. .bash_profile与.bashrc和.profile的区分概念

    在Linux系统中配置环境变量相关的文件主要有如下几个,很容易弄混的,这儿简单区分下: /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/p ...

  4. hdu1298 T9(手机输入法,每按一个数字,找出出现频率最高的字串,字典树+DFS)

    Problem Description A while ago it was quite cumbersome to create a message for the Short Message Se ...

  5. wifi定位原理

    wifi定位和手机基站定位类别似,两者都需要收集wifi位置信息接入点. 其实WIFI奇妙,它靠的是侦測附近周围全部的无线网路基地台 (WiFi Access Point) 的 MAC Address ...

  6. Android 高仿 频道管理----网易、今日头条、腾讯视频 (可以拖动的GridView)附源码DEMO

    距离上次发布(android高仿系列)今日头条 --新闻阅读器 (二) 相关的内容已经半个月了,最近利用空闲时间,把今日头条客户端完善了下.完善的功能一个一个全部实现后,就放整个源码.开发的进度就是按 ...

  7. Entity FramWork - 在VS里面直接创建表,并同步到数据库

    前面具体添加什么直接看: 1.Entity - 使用EF框架进行增删改查 - 模型先行 2.Entity - 使用EF框架进行增删改查 - 数据库先行 然后: 然后右键,可以添加[实体],也就是表.之 ...

  8. smartforms初始化

    smartforms  第一次打开的页面是和prd环境下的一样,需要跑一个程序才能编辑

  9. 关于Smartforms换页的

    smartforms中有系统变量SFSY-PAGE是总页码,SFSY-FORMPAGES是当前页,可以最后的窗体中做个判断 1.把窗体设置成最终窗体 2.新增一个命令,当前页等于最后一页才输出改内容, ...

  10. POJ 1781 In Danger Joseph环 位运算解法

    Joseph环,这次模固定是2.假设不是固定模2,那么一般时间效率是O(n).可是这次由于固定模2,那么能够利用2的特殊性,把时间效率提高到O(1). 规律能够看下图: watermark/2/tex ...