Question: Python Question A modular number is written like v mod m, where v < m: if v m we replace v by v%m (the remainder

Python Question

A modular number is written like v mod m, where v < m: if v m we replace v by v%m (the remainder of v divided by m, guaranteeing v < m). So 13 mod 5 reduces to 3 mod 5 (because 13%5 is 3). The following class starts to define a Modular from two ints: v(alue) and m(odulus). For example, we can define a = Modular(13,5) which represents 3 mod 5; and b = Modular(1,5) which represents 1 mod 5. In this problem, assume that all modular numbers have a positive value and modulus. In the class below, write the method called by the repr function and overload the addition operator (so that we can compute a+b and a+2 and 2+a: note that addition is commutative). Adding two modular numbers results in a modular number, but only if the operands have the same modulus: the resulting modular number has that modulus, and its value is the sum off the values of its operands. Adding a modular number and an integer results in a modular number with the modular numbers modulus and a value that is the sum of the modular numbers value and the integer. So a+b is 4 mod 5 and a+2 is 5 mod 5 which init reduces to 0 mod 5. If we try to add two modular numbers with different moduli (the plural of modulus) raise an AssertionError exception; if we try to add a modular number to any other type, raise the TypeError exception.

class Modular: def __init__(self,value,modulus): self.value = value % modulus self.modulus = modulus

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!