Modular arithmetic calculation [closed] - math

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Is there any way to calculate (a mod c)*(b mod c)?
Knowing only:
a*b
c
d
((a mod c)*(b mod c)) mod c
(a mod d)*(b mod d)

No, it's impossible.
Here's a counterexample:
ab = 1225 = (5)(5)(7)(7)
c = 3
d = 5000
((a mod c)(b mod c)) mod c = 1
(a mod d)(b mod d) = 1225
If a=25 and b=49, then (a mod c)(b mod c)=(1)(1)=1
If a=35 and b=35, then (a mod c)(b mod c)=(2)(2)=4

Related

Linear Diophantine equation, Extended Euclidean Algorithm [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
suppose i want to find any value of x and y such that they satisfy x . W + y . D = P
this can be done by the following using extended euclidean algorithm
int exgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int g, xi, yi;
g = exgcd(b, a % b, xi, yi);
x = yi;
y = xi - (a / b * yi);
return g;
}
but this will be just some random x and y satisfying the equation
suppose i add an additional constraint that i want any x y z such that
x>=0 y>=0 z>=0 and x + y + z = n
how can i effectively (please share code/pseudocode if possible) find all such x y and z??
my question boils down to find any x and y (using extended euclidean algorithm) which
1) satisfy a linear equation
2) fall in a given range
here is the link to the question if you want
Ok so we have 2 equations and 3 unknowns, so doing some simple mathematics we can find the equations we need to solve
x * W + y * D + z * 0 = p
And
x + y + z = n
given
x,y,z >=0
So firstly we will reduce one unknown by looping through any one of the unknowns lets say z
We iterate through 0 - n for z and our new equations will be
x * w + y * d = p
And
x + y = m { m is n - z for value of z in current iteration
Now we have 2 equations and 2 unknowns
So our solution for x and y can now be reduced by substituting x in first equation
Which makes it
(m - y) * w + y * d = p
This can be reduced to
y = (p - m * w) / (d - w)
and
x = m - y
You can stop at first value where x and y are integers

Binary modulo operation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
My apologies, this question is now redirected to
this web page in math forum.
Empirically, I can know that (a+b+c) mod 2 = (a-b-c) mod 2.
e.g.,)
1+2+3 = 6, 6 mod 2 = 0
1-2-3 = -4, -4 mod 2 = 0
1+2+4 = 7, 7 mod 2 = 1
1-2-4 = -5, -5 mod 2 = 1
It seems that it is only possible when we use binary modulo (mod 2).
Is there any formal proof for this?
Not sure, why this ended up on SO. As James said in the comments, these questions should be asked on math.stackexchange But since it is here:
I a + b + c = a - b - c + 2(b + c)
II 2(b + c) ≡ 0 (mod 2), ergo
III a + b + c ≡ a - b - c (mod 2)
Edit, since it was requested: The generalisation of II would require n to be a divisor of 2 to fulfill
2(b + c) ≡ 0 (mod n)
for all b and c, which means that n is either 1 or 2.
The reason this works mod 2 is exactly because there are only two residues: 0 and 1. And thus it is true that for any x
x ≡ -x mod 2
Thus a + b ≡ a - b mod 2
Obviously this is not true for any other modulo operation. So for any other n > 2 you can create a simple counter-example for (a+b+c) ≡ (a-b-c) mod n:
a = n
b = 0
c = 1
(a + b + c) mod n = 1
(a - b - c) mod n = n - 1
Obviously n - 1 is not equal to 1 if n > 2. Actually most of the triplets (a, b, c) would be counter-examples for any n > 2.

How to calculate x^y mod z? [duplicate]

This question already has answers here:
Calculating (a^b)%MOD
(7 answers)
Closed 8 years ago.
I am wondering how to calculate x^y mod z. x and y are very large (can't fit in 64 bit integer) and z will fit 64 bit integer.
And one thing don't give answers like x^y mod z is same as (x mod z)^y mod z.
Here is the standard method, in pseudo-code:
function powerMod(b, e, m)
x := 1
while e > 0
if e % 2 == 1
x := (b * x) % m
e := e - 1
else
b := (b * b) % m
e := e / 2
return x
The algorithm is known as exponentiation by squaring or the square and multiply method.
If you are using Java, then turn x, y, and z into BigInteger.
BigInteger xBI = new BigInteger(x);
BigInteger yBI = new BigInteger(y);
BigInteger zBI = new BigInteger(z);
BigInteger answer = xBI.modPow(yBI,zBI);

what is wrong with this mathematical assumption [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have 2 3D points A and B I now assume the parametric equation like:
x = Ax + (Bx*t)
y = Ay + (By*t)
z = Az + (Bz*t)
So this can be refined to:
x = Ax + (Bx * ((y - Ay)/By));
is correct.
Given this I want to know what the coordinates of a point(on A-B) at height 0 are.
so I now do this:
float y = 0;
float t = ((y - Ay) / By)
float x = Ax + (Bx * t);
float z = Az + (Bz * t);
Is there anything mathematically wrong with this?
(my code is not doing what should be happening with this)
Thanks!
PS: The relevance of this question to programming:
In a game engine when projecting points(in this case corners of my view frustum) onto a plane such as the xz-plane with y = 0 this mathematical problem coincides with my game-programming
I would write them this way:
x = A*(1-t) + B*t
So x(0) = A and x(1) = B. Writing it this way assumes 0 <= t <= +1.
Another way to think about it is to assume -1 <= t <= +1. If you go that way the shape functions look like this:
x = A*(1-t)/2.0 + B*(1+t)/2.0
Once again x(-1) = A and x(+1) = B.
And it's easy to generalize to higher-order functions:
x(t) = A*t(t-1)/2 + C*(1-t^2) + B*t(t+1)/2
So x(0) = A, x(0.5) = C, x(1) = B.
I think your parametrization is wrong:
x = Ax + (Bx*t)
...
But it should be:
x = Ax + ((Bx-Ax)*t)
...

x = 27 * 24 \ 4 / 2 – 4 + 2 [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Can someone help with the following MATH question.
Keep in mind the operator precedence.
x = 27 * 24 \ 4 / 2 – 4 + 2
Arithmetic and logical operators are evaluated in the following order of precedence:
Exponentiation (^)
Negation (-)
Multiplication and division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
What is the result?
x = ((27 * 24) \ (4 / 2)) - 4 + 2
= (648 \ 2) - 4 + 2
= 324 - 4 + 2
= 322

Resources