Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving modulus operator to the other side of the equation

Tags:

math

modulus

I have a mathematical problem that is part of my programming problem

I have a statement like

a = b%30;

How can I calculate b in terms of a?

I gave it a thought but couldn't figure it out.

like image 314
Old Person Avatar asked Jan 03 '14 22:01

Old Person


People also ask

What does the modulus (%) operator do?

The modulo operator, denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division. produces the remainder when x is divided by y.

Can modulo reverse?

An additive inverse of a modulo m always exists for every a and m . It's given by any number of the form –a + k * m , where k is an integer. We usually want to find the inverse in the range {0, ... , m - 1} , i.e., in the set of remainders of division by m .

How do you reverse a mod in Python?

You can't really reverse modulo arithmetic like that. Modulo arithmetic returns basically equivalence groups. Think about a calendar... Calendars ( and clocks ) are mod 12.

What's the opposite of modulus?

If you want to return the opposite, the integer part of a division, in that case you have to use the QUOTIENT function.


2 Answers

By definition,

b == 30*n + a

for some integer n.

Note that there are multiple b values that can give you the same a:

>>> b = 31
>>> b % 30
1
>>> b = 61
>>> b % 30
1
like image 193
arshajii Avatar answered Jan 03 '23 13:01

arshajii


First, obviously, there are in general several solutions for b for a given a.

If % is the remainder operator found in most programming languages, then the sign of a is critical. You know that this is a website for programming questions, right?

If |a|>=30, then there are no solutions

If a = 0, the solutions are the multiples of 30.

If 0 < a < 30, the solutions are all the b such that b = 30 * k + a for some positive integer k.

If -30 < a < 0, the solutions are all the b such that b = - (30 * k - a) for some positive integer k.

like image 34
Pascal Cuoq Avatar answered Jan 03 '23 12:01

Pascal Cuoq