当前位置: 首页 > news >正文

Mod in math

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 } RBA=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+KB) 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)AB (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.

相关文章:

  • js keyup、keypress和keydown事件 详解
  • 云栖问答送的淘公仔收到啦
  • struts2自己定义类型转换器
  • DJANGO的requirements的运用
  • 糖葫芦照样吃
  • RESTful三理解
  • 春运学生与民工
  • c11通过share_from_this构造另一类对象抛异常
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • php底层开发框架, yaf,swoole,hiphop
  • 《仙剑奇侠传4》仙剑问答全答案
  • URL介绍及处理
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • 半平面交模板
  • 观察站模式
  • [iOS]Core Data浅析一 -- 启用Core Data
  • 4. 路由到控制器 - Laravel从零开始教程
  • Codepen 每日精选(2018-3-25)
  • eclipse(luna)创建web工程
  • JS专题之继承
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • node入门
  • Redux 中间件分析
  • Spark RDD学习: aggregate函数
  • SpingCloudBus整合RabbitMQ
  • Windows Containers 大冒险: 容器网络
  • 理解在java “”i=i++;”所发生的事情
  • 聊一聊前端的监控
  • 前端相关框架总和
  • 使用 5W1H 写出高可读的 Git Commit Message
  • 通信类
  • 物联网链路协议
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • 自制字幕遮挡器
  • 阿里云重庆大学大数据训练营落地分享
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • ​LeetCode解法汇总518. 零钱兑换 II
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (145)光线追踪距离场柔和阴影
  • (floyd+补集) poj 3275
  • (pojstep1.1.2)2654(直叙式模拟)
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (九)One-Wire总线-DS18B20
  • (四) Graphivz 颜色选择
  • (转)http协议
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • (转)程序员技术练级攻略
  • (转)关于pipe()的详细解析
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .gitignore文件—git忽略文件
  • .net mvc 获取url中controller和action
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题