1. Modulo %
key concept for % is congruence, two numbers a, b are congruent on n, if and only if they have the same modular remainder after mod by n
if a % n = b, then we say a and b is congruent on n, marked as a ≡ b (mod n)
Key Attributes of Modulo Congruence:
- Reflexivity: a ≡ a (mod n)
- Symmetry: If a ≡ b (mod n), then b ≡ a (mod n)
- Transitivity: If a ≡ b (mod n) and b ≡ c (mod n), then a ≡ c (mod n)
- Addition: If a ≡ b (mod n) and c ≡ d (mod n), then a + b ≡ c + d (mod n)
- Multiplication: If a ≡ b (mod n) and c ≡ d (mod n), then a * b ≡ c * d (mod n)
- Divisibility: If a ≡ b (mod n), then a - b is divisible by n, in other words: a - b | n
- Power: If a ≡ b (mod n), then ak ≡ bk (mod n)
2. Greatest Command Divisor
Euclidean Algorithm:
gcd(a, 0) = a gcd(a, b) = gcd(b, a % b)
int gcd(int a, int b) { while(b != 0) { int temp = b; b = a % b; a = temp; } return a; }