Skip to main content

Basic Modular Arithmetic

In simple terms, Modular Arithmetic calculates the remainder of anything divided by anything. The later is called modulus. e.g. ( 15 / 7 ) Quotient: 2, Remainder 1. 

In greater sense, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" upon reaching a certain value—the modulus ( src: Wikipedia ).
Take a look at hour clock. After 12:59 pm, we say 1:00 pm, because we modulo the hours by 12. 13 pm % 12 is 1 pm. (%) represents modulus operator.
Examples: 5 % 2 = 1, 14458948 % 25 = 23

a≡b (mod n)
This says that a is congruent to b modulo n. It means both a and b has same remainder when divided by n. e.g. 38≡14 (mod 12)

Commonly Used Properties:
  • Reflexivity: a ≡ a (mod n)
  • Symmetry: a ≡ b (mod n) if and only if b ≡ a (mod n)
  • Transitivity: If a ≡ b (mod n) and b ≡ c (mod n), then a ≡ c (mod n)
  • If a1 ≡ b1 (mod n) and a2 ≡ b2 (mod n), or if a ≡ b (mod n), then:a + k ≡ b + k (mod n) for any integer k (compatibility with translation)
  • k a ≡ k b (mod n) for any integer k (compatibility with scaling)
  • a1 + a2 ≡ b1 + b2 (mod n) (compatibility with addition)
  • a1 – a2 ≡ b1 – b2 (mod n) (compatibility with subtraction)
  • a1 a2 ≡ b1 b2 (mod n) (compatibility with multiplication)
  • ak ≡ bk (mod n) for any non-negative integer k (compatibility with exponentiation)
Simple C program to perform Modulo:

In CS, modular arithmetic is used in many algorithms where integers become too big to handle.

For more details:

 modulo C.

Comments

Popular posts from this blog

SPOJ - GSS1

Considering input series: { 4 , -10 , 3 , 100 , -20 , 1 } Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y } A node contains-  [ START & END is a node's segment limit ] Prefix is the maximum sum starting at START, end can be anywhere. There are two possibilities of the maximum. One, node's leftChild's prefix or two, adding leftChild's sum + rightChild's prefix. (which will make the prefix contiguous) Suffix is the maximum sum ending at END, start can be anywhere. There's two possibility of the maximum. One, node's rightChild's already calculated suffix or two, add rightChild's sum + leftChild's suffix   (which will make the suffix contiguous). Sum : leftChild's sum + rightChild's sum. MAX Maximum of  -  prefix (result is in the node, starts from START but doesn't end in END ) suffix  (result is in the node, doesn't start from START but surely ends in END ) leftChild's max ( result is in left ...

Trailing Zeroes of n!

Find the number of trailing zeroes in 122! ?  That's a big number right? The solution is pretty simple. Ask yourself when does any multiplication results in a trailing zero? The answer is (2x5) = 10, leaves one zero behind. The factorial of 122 is a multiplication which you already know. The expansion is, 98750442008336013624115798714482080125644041369 78359605958470050267671457205014364903379642774 50422940710230505796264047365129395968426948958 2137821062001338805474721479524352 0000000000000 000000000000000 So, we have to find the number of multiples of 5 from the expansion and they are 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120. So there are 24 multiples of 5. then again, 25= 5 x 5 , which has another extra factor of  5 to count. So how many multiples of 25 from 1 to 122? (122 / 25) = 4 (taking the only integer) So, add that 4 to the earlier count. 24 + 4 = 28 , so we have 28 trailing zero in 122! Let's take a num...