There are several theorems associated with modular arithmetic that are used in various applications:
a = b x q + r
where 0 <= r < b
For instance, if a=40 & b=7 then q=5 & r=5 :
40= 7 x 5 + 5
(a + b) mod m = ((a mod m) + (b mod m)) mod m
&
(a - b) mod m = ((a mod m) - (b mod m)) mod m
For instance,
if a=10, b=50 & m= 7 :
(10 + 50) mod 7 = ((10 mod 7) + (50 mod 7)) mod 7
60 mod 7 = (3 + 1) mod 7
4 = 4
(a x b) mod m = ((a mod m) x (b mod m)) mod m.
For instance,
if a=10, b=50 & m= 7 :
(10 x 50) mod 7 = ((10 mod 7) x (50 mod 7)) mod 7
500 mod 7 = (3 x 1) mod 7
3 = 3
(a / b) mod m = (a x (inverse of b if exists)) mod m
If b and m are relatively prime, then the modular inverse of b modulo m exists and can be found using the extended Euclidean algorithm.
a^b mod m =
2^4 mod 7 = 16 mod 7
= 2
Let us look at some codes to solve the above laws in a programmatic way:
public static int modAdd(int a, int b, int m) {
return ((a % m) + (b % m)) % m;
}
public static int modSub(int a, int b, int m) {
return ((a % m) - (b % m) + m) % m;
}
public static int modMul(int a, int b, int m) {
return ((a % m) * (b % m)) % m;
}
public static int modDiv(int a, int b, int m) {
int inv = modInverse(b, m);
return (a * inv) % m;
}
public static int modExp(int a, int b, int m) {
int res = 1;
while (b > 0) {
if (b % 2 == 1) {
res = (res * a) % m;
}
a = (a * a) % m;
b = b / 2;
}
return res;
}
These are the properties of modular arithmetic:
k
, you need to find the length of the smallest positive integer n
such that n
is divisible by k
, and n
only contains the digit 1
. Return the length of n
. If there is no such n
, return -1.i.e,
Input: k = 3 Output: 3 Explanation: The smallest answer is n = 111, which has length 3.
Solution:
class Solution {
public int smallestRepunitDivByK(int k) {
if (k % 2 == 0 || k % 5 == 0) {
// if k is divisible by 2 or 5, n will never be divisible by k
return -1;
}
int num = 1;
int length = 1;
while (num % k != 0) {
num = (num * 10 + 1) % k;
length++;
}
return length;
}
}
We did this using modular arithmetic as follows:
num
with the value 1, and a variable length
with the value 1.num
is not divisible by k
, do the following:num
to be (num * 10 + 1) % k
.length
by 1.num
is divisible by k
, return length
. Otherwise, return -1.Note: also read about Problems based on bit manipulation
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…