-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModuloInteger.py
More file actions
22 lines (18 loc) · 880 Bytes
/
ModuloInteger.py
File metadata and controls
22 lines (18 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ModuloInteger:
def __init__(self, value, modulus):
self.value = value % modulus
self.modulus = modulus
def __add__(self, other):
if self.modulus != other.modulus:
raise ValueError("Moduli must be the same for addition.")
return ModuloInteger((self.value + other.value) % self.modulus, self.modulus)
def __sub__(self, other):
if self.modulus != other.modulus:
raise ValueError("Moduli must be the same for subtraction.")
return ModuloInteger((self.value - other.value) % self.modulus, self.modulus)
def __mul__(self, other):
if self.modulus != other.modulus:
raise ValueError("Moduli must be the same for multiplication.")
return ModuloInteger((self.value * other.value) % self.modulus, self.modulus)
def __str__(self):
return str(self.value)