[BASICS] Modular Arithmetic, what is it?
Background
Sadly, not many people enjoy Mathematics anymore (especially my class mates back in high school). Anyway, I have been reading a lot into Cryptography and various encryption algorithms recently and I was reading a bit about modular arithmetic and how it utilised.
Some of you may be thinking "What is this... why should we care?" Well, the correct answer (I think) that you work with it everyday, so it is probably worth knowing!
Modular Arithmetic is an integer (whole number) based system where numbers "wrap around" when they hit a maximum value. This will be explained with examples later.
The easiest way to see how this works in everyday life is by looking at a clock. A clock has 12 values on it, yet a day has 24 hours, correct? When working with 12 hours only the values "wrap" when they go above 12, so instead of 13 we see 1. 14 we see 2, etc.
How does this work in terms of the maths?
For anyone who has done a little bit of programming before, you have probably seen the % operator used for various functions. For those who haven't, the % symbol is known as the modulo symbol. It is used in a similar fashion as a division symbol (/) but instead of returning the result of the division, it returns the remainder. This is essentially modular arithmetic.
Examples:10 * 5 = 50 (base 10)
10 * 5 = 0 (mod 5) because 10 * 5 = 50 / 5 = 10 remainder 0. So 0 is the result
3 + 5 = 8 (base 10)
3 + 5 = 3 (mod 5) because 3 + 5 = 8 / 5 = 1 remainder 3. So 3 is the result
Hopefully the examples make the method of modular arithmetic easy to view. Something to note, when counting the values start from 0. So working in mod 5, we would count 0, 1, 2, 3, 4. 5 then becomes 0 again, this is the "wrap around".
Coding a tool to help with mod calculations [Python 2.7]
After reading up on the subject and setting myself a few small challenges, I decided to make a quick tool using Python to help me out. The code is relatively self explanatory, but I will do a simple pseudo representation first.
Ask user for mod value
Ask user for value A
Ask user for value B
(for both adding and multiplying)
Calculate the result of A and B together
If the result is greater or equal to the mod value
Calculate the remainder
Show the results on screen
I'm not the most fluent Python coder, so if there are things that can be done easier please comment below and let me know!
# modular arithmetic toolset
import math
import sys
def calculator(mod_value, a, b):
# add
print "-- Addition --"
added = a + b
added_mod = added
if added >= mod_value:
added_mod = get_mod_value(added, mod_value)
print str(a) + " + " + str(b) + " = " + str(int(added_mod))
# multiplied
print "-- Multiplied --"
multiplied = a * b
multiplied_mod = multiplied
if multiplied >= mod_value:
multiplied_mod = get_mod_value(multiplied, mod_value)
print str(a) + " * " + str(b) + " = " + str(int(multiplied_mod))
def get_mod_value(total, mod_val):
added_mod = total - (math.floor(total / mod_val) * mod_val)
return added_mod
print "This tool will compute the mod value of your specified input."
print "Current operators: + *"
mod_value = int(raw_input("Enter your mod value: "))
value_a = int(raw_input("Enter value A: "))
value_b = int(raw_input("Enter value B: "))
calculator(mod_value, value_a, value_b)
Follow me for more programming related posts, as well as cybersecurity and maths! Finally, criticism is always welcome so please tell me where and how I can improve to make my posts more enjoyable for all interested parties! Thanks
Welcome to steemit @justblink!
If you like to post and read about STEM-related topics you can check out the @steemstem community project and the #steemstem tag.
Enjoy your steemit experience! See you around!
I will do :D thanks for this!
Want to stay on top of your game? You should definitely start learning AI @neurallearner