Number Theory
Basics

Introduction

Number theory is a branch of mathematics that deals with the properties and relationships of numbers, particularly integers. It involves the study of prime numbers, divisibility, and the structure of the set of integers. Number theory has many applications in cryptography, computer science, and pure mathematics.

Modular arithmetic

Modular arithmetic is a system of arithmetic for integers where numbers "wrap around" upon reaching a certain value called the modulus. It is often described as "clock arithmetic," where after reaching a certain value, the counting starts again from zero.

For example, in modular arithmetic, if the modulus is 12 (like a clock), after 11 comes 0, then 1, 2, etc.

Mathematically, we write modular arithmetic as:

ab (mod m)a \equiv b \ (\text{mod} \ m)

This means that when aa is divided by mm, the remainder is bb. In other words, aa and bb are congruent modulo mm. It is also often represented as %\% symbol

Properties of modulo arithmetic

  • (a+b)%m=((a%m)+(b%m))%m(a + b) \,\% \,m = ((a \,\%\, m) + (b \,\%\, m)) \,\%\,m
  • (ab)%m=((a%m)(b%m)+m)%m(a - b) \,\% \,m = ((a \,\%\, m) - (b \,\%\, m) + m) \,\%\,m
  • (ab)%m=((a%m)(b%m))%m(a * b) \,\% \,m = ((a \,\%\, m) * (b \,\%\, m)) \,\%\,m
  • (a/b)%m=((a%m)(b1%m))%m(a \,/\, b) \,\% \,m = ((a \,\%\, m) * (b ^ {-1} \,\%\, m)) \,\%\,m

Here b1b^{-1} is multiplicative modulo inverse of bb and mm

Example

  • Assume aa = 101710 ^ {17} , bb = 101510 ^ {15} and cc = 109+710 ^ {9} + 7 and we need to find (ab)%c(a * b) \,\%\,c. This would easily overflow in int or long resulting in incorrect results. On applying - (ab)%m=((a%m)(b%m))%m(a * b) \,\% \,m = ((a \,\%\, m) * (b \,\%\, m)) \,\%\,m we would be able to solve this correctly here is how -

  • 1017%(109+7)=30000000710 ^ {17} \, \% \, (10 ^ {9} + 7) = 300000007
    1015%(109+7)=99300000710 ^ {15} \, \% \, (10 ^ {9} + 7) = 993000007
    300000007993000007=297900009051000049300000007 * 993000007 = 297900009051000049
    297900009051000049%(109+7)=965700007297900009051000049 \,\% \, (10 ^ {9} + 7) = 965700007