If I have x= 11 and y = 6 and I want to calculate
(w*x)mod(y) = 1 . In other words how can I calculate the number that if multiplied by 11 and then modulus 6 the result 1. In this case w should be equal to 5.
Is there anyway I can calculate the w in a method using Euclidean algorithm in java?
Thank you!
There is a theorem that says that the linear congruence a * x = b (mod n), where a, b, and n are integers, has a solution if and only if gcd(a, n) = 1.
Since gcd(11,6) = 1, which is simply because 11 is a prime number, your equation is indeed solvable.
To answer the question, no, you cannot solve the linear congruence using Euclid's algorithms---however, you can do that using extended Euclid's algorithm---, but you can use it so verify that the equation is solvable.
Once you find that gcd(a,n)=1, you compute the solution as x = b*r mod n, where r = a^-1 (mod n). To compute the inverse of a, which here we denoted r, you can use the extended Euclidean algorithm (abbreviated EEA).
If gcd(a,n)=1, then EEA, given a and n, computes r and s such that a*r + n*s = 1. We claim that r is the inverse of a modulo n. Once you have r, you compute x = b * r mod n.
These algorithms are nicely described in the book Introduction to Algorithm by Cormen et al.
I have to multiply two large numbers like x and y and then find the mod of result by p without using BigIntegerlike :
public static char[] x = {'1','1','5','7','9','2','0','8','9','2','3','7','3','1','6','1','9','5','4','2','3','5','7','0','9','8','5','0','0','8','6','8','7','9','0','7','8','5','3','2','6','9','9','8','4','6','6','5','6','4','0','5','6','4','0','3','9','4','5','7','5','8','4','0','0','7','9','0','8','8','3','3','7','6','6','4','4','7'};
public static char[] y = {'1','1','5','7','9','2','0','8','9','2','3','7','3','1','6','1','9','5','4','2','3','5','7','0','9','8','5','0','0','8','6','8','7','9','0','7','8','5','3','2','6','9','9','8','4','6','6','5','6','4','0','5','6','4','0','3','9','4','4','3','7','4','8','7','3','8','7','4','7','3','1','9','0','6','9','7','4','3'};
public static char[] p = {'1','1','5','7','9','2','0','8','9','2','3','7','3','1','6','1','9','5','4','2','3','5','7','0','9','8','5','0','0','8','6','8','7','9','0','7','8','5','3','2','6','9','9','8','4','6','6','5','6','4','0','5','6','4','0','3','9','4','5','7','5','8','4','0','0','7','9','0','8','8','3','4','6','7','1','6','6','3'};
I find out how to multiply x and y but I don't know how to compute mod of two large numbers
Can anyone help me?
Can you compare x and y?
If you can compare them, you can easily implement mod algorithm.
Pseudo Code
function mod(a , b) {
while( a > b ) { a = a - b; }
return a;
}
I dont know java, this is just pseudo code for algorithm.
But if you have implemented subtract and compare functions, you can use them here.
I have a homework assignment that asks of me to check, for any three numbers, a,b,c such that 0<=a,b,c<=10^16, if I can reach c by adding a and b to each other. The trick is, with every addition, their value changes, so if we add a to b, we would then have the numbers a and a+b, instead of a and b. Because of this, I realized it's not a simple linear equation.
In order for this to be possible, the target number c, must be able to be represented in the form:
c = xa + yb
Through some testing, I figured out that the values of x and y, can't be equal, nor can both of them be even, in order for me to be able to reach the number c. Keeping this in mind, along with some special cases involving a,b or c to be equal to zero.
Any ideas?
EDIT:
It's not Euclid's Algorithm, it's not a diophantine equation, maybe I have mislead you with the statement that c = xa + yc. Even though they should satisfy this statement, it's not enough for the assignment at hand.
Take a=2, b=3, c=10 for example. In order to reach c, you would need to add a to b or b to a in the first step, and then in the second step you'd get either : a = 2, b = 5 or a = 5, b = 3, and if you keep doing this, you will never reach c. Euclid's algorithm will provide the output yes, but it's clear that you can't reach 10, by adding 2 and 3 to one another.
Note: To restate the problem, as I understand it: Suppose you're given nonnegative integers a, b, and c. Is it possible, by performing a sequence of zero or more operations a = a + b or b = b + a, to reach a point where a + b == c?
OK, after looking into this further, I think you can make a small change to the statement you made in your question:
In order for this to be possible, the target number c, must be able to
be represented in the form:
c = xa + yb
where GCD(x,y) = 1.
(Also, x and y need to be nonnegative; I'm not sure if they may be 0 or not.)
Your original observation, that x may not equal y (unless they're both 1) and that x and y cannot both be even, are implied by the new condition GCD(x,y) = 1; so those observations were correct, but not strong enough.
If you use this in your program instead of the test you already have, it may make the tests pass. (I'm not guaranteeing anything.) For a faster algorithm, you can use Extended Euclid's Algorithm as suggested in the comments (and Henry's answer) to find one x0 and y0; but if GCD(x0,y0) ≠ 1, you'd have to try other possibilities x = x0 + nb, y = y0 - na, for some n (which may be negative).
I don't have a rigorous proof. Suppose we constructed the set S of all pairs (x,y) such that (1,1) is in S, and if (x,y) is in S then (x,x+y) and (x+y,y) are in S. It's obvious that (1,n) and (n,1) are in S for all n > 1. Then we can try to figure out, for some m and n > 1, how could the pair (m,n) get into S? If m < n, this is possible only if (m, n-m) was already in S. If m > n, it's possible only if (m-n, n) was already in S. Either way, when you keep subtracting the smaller number from the larger, what you get is essentially Euclid's algorithm, which means you'll hit a point where your pair is (g,g) where g = GCD(m,n); and that pair is in S only if g = 1. It appears to me that the possible values for x and y in the above equation for the target number c are exactly those which are in S. Still, this is partly based on intuition; more work would be needed to make it rigorous.
If we forget for a moment that x and y should be positive, the equation c = xa + yb has either no or infinitely many solutions. When c is not a multiple of gcd(a,b) there is no solution.
Otherwise, calling gcd(a,b) = t use the extended euclidean algorithm to find d and e such that t = da + eb. One solution is then given by c = dc/t a + ec/t b.
It is clear that 0 = b/t a - a/t b so more solutions can be found by adding a multiple f of that to the equation:
c = (dc + fb)/t a + (ec - af)/t b
When we now reintroduce the restriction that x and y must be positive or zero, the question becomes to find values of f that make x = (dc + fb)/t and y = (ec - af)/t both positive or zero.
If dc < 0 try the smallest f that makes dc + fb >= 0 and see if ec - af is also >=0.
Otherwise try the largest f (a negative number) that makes ec - af >= 0 and check if dc + fb >= 0.
import java.util.*;
import java.math.BigInteger;
public class Main
{
private static boolean result(long a, long b, long c)
{
long M=c%(a+b);
return (M%b == 0) || (M%a == 0);
}
}
Idea:c=xa+by, because either x or y is bigger we can write the latter equation in one of two forms:
c=x(a+b)+(y-x)b,
c=y(a+b)+(x-y)a
depending on who is bigger, so by reducing c by a+b each time, c eventually becomes:
c=(y-x)b or c=(x-y)b, so c%b or c%a will evaluate to 0.
please I am trying to understand matrix computation.
and my question may seem simple but please need an answer
can some briefly explain to me what is an RHS vector.
I often see it used in the Apache commons math library
for example i got this from a stackoverflow page:
public class LinearAlgebraDemo
{
public static void main(String[] args)
{
double [][] values = {{1, 1, 2}, {2, 4, -3}, {3, 6, -5}};
double [] rhs = { 9, 1, 0 }; /* RHS Vector */
RealMatrix a = new Array2DRowRealMatrix(values);
DecompositionSolver solver = new LUDecompositionImpl(a).getSolver();
RealVector b = new ArrayRealVector(rhs);
RealVector x = solver.solve(b);
RealVector residual = a.operate(x).subtract(b);
double rnorm = residual.getLInfNorm();
}
}
can someone explain this code to me and especially the RHS vector and its purpose.
thank you very much.
You matrix equation looks like this:
Ax = b
where A is a matrix with m rows and n columns, x is a column vector of m unknowns, and b is another column vector (aka The Right-Hand Side) of m known values. It's on the right hand side of the equals sign - hence the name.
If I gave you a simple equation with two numbers and an unknown value x, you'd know exactly how to solve it:
Ax = b -> x = b/A
Think of this as solving for x by multiplying both sides of the equation by the inverse of A.
In this case it's more complicated, because dividing by a matrix means inverting it.
You're not going to invert the matrix; you're going to create something called an LU decomposition of the matrix A. You should read about what that is and why it's better than calculating a full inverse if you're interested.
RHS is a common mathematical abbreviation for "right hand side". Here it appears that you are solving a system of linear equations Ax = b where A is an n x n matrix and x and b are n-dimensional column vectors. If you don't understand this terminology, then I suggest you study up on linear algebra.
As for the code, rhs is an array which is used to initialize the elements of the vector b. Similarly, the 2D array values is used to initialize the elements of the matrix A (actually a reference variable named a).