Linear Diophantine equation, Extended Euclidean Algorithm [closed] - math

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

Related

calculate the sum of a series with limit of x tends to 1

I want to calculate the sum of the series as below
Lim
X->1 (2/3 - x/3 -(x^2)/3 +(x^3)*2/3 -..). I am not sure whether we have a formula for finding the sum of this kind of series. Tried a lot but couldn't find any. Any help is appreciated.
This seems to be more maths than computing.
It factorises as (1 + x^3 + x^6 + ...)(2 - x - x^2)/3
If x = 1-d (where d is small), then to first order in d, the (2 - x - x^2) term becomes (2 - (1-d) - (1-2d)) = 3d
And the (1 + x^3 + x^6 + ...) term is a geometric progression, with sum 1/(1-x^3), or here 1/(1-(1-d)^3), and the denominator to first order in d is (1 - (1-3d)) = 3d
Hence the whole thing is (1/3d) (3d) / 3 = 1/3
But we can also verify computationally with a value close to 1 (Python code here):
x = 0.999999
s = 0
f = (2 - x - x*x) / 3.
x3 = x ** 3
s_prev = None
while s != s_prev:
s_prev = s
s += f
f *= x3
print(s)
gives:
0.33333355556918565

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)
...

collision detection between two lines

This is a fairly simple question. I need need an equation to determine whether two 2 dimensional lines collide with each other. If they do I also need to know the X and Y position of the collision.
Put them both in general form. If A and B are the same then they're parallel. Otherwise, create two simultaneous equations and solve for x and y.
Let A and B represented by this parametric form : y = mx + b
Where m is the slope of the line
Now in the case of parallelism of A and B their slope should be equal
Else they will collide with each other at point T(x,y)
For finding the coordinates of point T you have to solve an easy equation:
A: y = mx + b
B: y = Mx + B
y(A) = y(B) means : mx + b = Mx + B which yields to x = (B - b)/(m - M) and by putting
the x to the line A we find y = ((m*(B - b))/(m - M)) + b
so : T : ((B - b)/(m - M) , ((m*(B - b))/(m - M)) + b)

Cut one stick twice to turn it to be three sticks, what is the probability that the three sticks form a triangle? [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 11 years ago.
Improve this question
Well it is not a program problem. Is there any hint for such quiz?
I am thinking about focusing on two random R1, R2, both of which is in range (0, 1). and supposing R2 > R1
and then fulfill two equation:
R1 + (1 - R2) > R2 - R1 // two sticks sum longer then the rest one
|R1 - (1 - R2)| < R2 - R1 // the difference of these two should be shorter the rest one
but I cannot move further...
Think of (r1, r2) as a point in the unit square.
Which part of the unit square is allowed for r2 > r1?
Which part of that leads to three lengths that can form a triangle?
The answer is 1/4.
Here is the explanation.
Let x is the length of the leftmost stick and y is the length of the rightmost stick.
Then the middle stick has length n-x-y, if the original stick's length was n.
The possible values for x,y are those for which:
x > 0
y > 0
x + y < n
In the plane Oxy this is equivalent to say that the point (x,y) lies within the triangle with vertices (0, 0), (n, 0), (0, n).
Now these three numbers (x, y, n-x-y) form a triangle if all of the three are satisfied:
x + y > n - x - y <=> x + y < n/2
x + (n - x - y) > y <=> y < n/2
y + (n - x - y) > x <=> x < n/2
Again in the Oxy plane these are satisfied when the point (x,y) lies within the triangle with vertices (0, n/2), (n/2, n/2), (n/2, 0).
The area of this triangle is a quarter of the area of the (0, 0), (n, 0), (0, n) triangle, since it's the 'middle' triangle (whose vertices are the midpoints) of the bigger one.
Here is a simple C# program to verify the answer:
Random r = new Random();
int count = 0, total = 0, tries = 1000000;
double x, y;
for (int i = 0; i < tries; i++)
{
x = r.NextDouble();
y = r.NextDouble();
if (x + y > 0.5 && x < 0.5 && y < 0.5) ++count;
if (x + y < 1.0) ++total;
}
Console.WriteLine((double)count / total);
I have just made a program to verify it, and I found it is 1/4:
class Program
{
static void Main(string[] args)
{
int nIsTriangle = 0;
Random ran = new Random(0);
int nTry = 1000000;
for (int i = 0; i < nTry; i++)
{
double r1 = ran.NextDouble();
double r2 = ran.NextDouble();
if (Check(r1, r2)) nIsTriangle++;
}
Console.WriteLine((double)nIsTriangle / (double)nTry);
Console.ReadKey();
}
static bool Check(double r1, double r2)
{
double first = Math.Min(r1, r2);
double second = Math.Abs(r1 - r2);
double third = 1 - Math.Max(r1, r2);
bool conditionA = (first + second) > third;
bool conditionB = Math.Abs(first - second) < third;
return conditionA && conditionB;
}
}

Resources