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.
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.
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 .
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.
If you want to return the opposite, the integer part of a division, in that case you have to use the QUOTIENT function.
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With