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 1 year ago.
Improve this question
There is a problem I am supposed to solve that is normally easy, but it has a catch.
There are 2 types of candy. One type weighs m1 kg and is sold for s1 Euro. A second type weighs m2 kg and is sold for s2 Euro. All numbers are integers.
The question is, which type of candy costs more per kg?
The catch is, you can't use divide operation at all, neither / nor %.
For example, if we have the numbers as m1=2, s1=17, m2=3, and s2=14 then the answer needs to be that the first candies are more expensive as 17/2=8.5 and 14/3=4.(3).
As I am a C++ student, I am restricted to use only that which has been taught so far in the class to determine the more expensive candy. The only thing we learned so far was + - / * % and if statement with else. Also == > < && ||.
Compare X ≡ s1 * m2 with Y ≡ s2 * m1. If X > Y, then s1 / m1 > s2 / m2.
No division is required to do the comparison.
The caveat to this solution is that s1, s2, m1, and m2 should all have the same sign, and m1 and m2 should be non-zero.
Let's assume all the values are positive integers (hence, greater than 0). Consequently m1 * m2 is positive as well. Let z be the number such that:
z + (s1 / m1) = s2 / m2
By multiplying by m1 * m2 on both sides, we get:
z' + (s1 * m2) = s2 * m1 ∵ z' ≡ z × (m1 * m2)
Since z and z' have the same sign, the relational order of s1 / m1 and s1 / m2 is the same as the relational order of s1 * m2 and s2 * m1.
Related
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 7 months ago.
Improve this question
Given line1, line2, and distance d, I want to write a program to calculate the point on Line2 that is perpendicular to Line1, and the perpendicular distance is d.
Does anyone know where to start?
In C++ or any other programming language.
Thanks in advance.
Normalize line1 equation
L = sqrt(a1*a1+b1*b1)
a11 = a1 / L
b11 = b1 / L
c11 = c1 / L
Now for arbitrary point (x,y) length of perpendicular projection onto line1 is
d = a11*x + b11*y + c11
a11*x + b11*y + (c11-d) = 0
for vertical line2 case (b2==0) and (b11 != 0)
x = c2/a2
a11*c2/a2 + b11*y + (c11-d) = 0
y = -(a11*c2/a2 + (c11-d)) / b11
so needed point is (c2/a2, -(a11*c2/a2 + (c11-d)) / b11)
For general case, if (a11*b2 != b11*a2) (non-parallel line case):
y = - (a2*x + c2)/b2
a11*x - b11*(a2*x+c2)/b2 + (c11-d) = 0
a11*x - b11*a2*x/b2 + (c11 - d - b11*c2/b2) = 0
x*(a11 - b11*a2/b2) = (d + b11*c2/b2 - c11)
x = (d + b11*c2/b2 - c11) / (a11 - b11*a2/b2)
and point is ((d + b11*c2/b2 - c11) / (a11 - b11*a2/b2), y for this x)
#463035818_is_not_a_number is right, you need to do the math first.
I would suggest to find the intersection point and the angle first: https://www.cuemath.com/geometry/intersection-of-two-lines/
Then you can calculate 'c' and then you can write sin(alpha)=d/c equation with (x,y). So you'll have line2 and the equation that you get in terms of x and y which gives you 2 equations with 2 unknowns. hope this helps.
Given a line equation for line1 with y1 = m1 * x1 + b1 we move this line in normal direction with the given distance d and have then y1 = m1 * x1 + (b1 +/- d). As you can see we could have 2 possible solutions if the lines are not parallel.
Now with the new line we search the intersection with line2, which is given by the formula:
Line Equations:
Line_1_with_d => y1 = m1 * x1 + (b1 +/- d)
Line_2 => y2 = m2 * x2 + b2
Intersection Equations:
Point_x = (b2 - (b1 +/- d)) / (m1 - m2) (you will get here a problem if they are parallel)
Point_y = (m1 * (b2 - (b1 +/- d))) / ((m1 - m2) + (b1 +/- d))
If the lines are parallel (which you should check first) you can test if the given distance d matches the distance of the lines:
d = (|b2 - b1|) / (sqrt(m^2 + 1)) (m1 == m2 if parallel)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Currently I was trying to solve a problem with the condition below:
1. giving an elliptic curve y^2 = x^3 + ax + b
2. the user will input a, b and two points that exactly on the curve.
To put it simply, what I really need to do is make a secant line of the graph with the two points P and Q and try to check that if there is any point of intersection existing. If it exists, get the x and y of this point. I'm so struggled to solve this problem. Could someone give me some hints?
I'd homogenize this to x^3 + axz^2 + bz^3 - y^2z = 0 and points P = [Px : Py : Pz] and Q = [Qx : Qy : Qz]. Then any point R = λP + μQ with (λ, μ) ≠ (0, 0) lies on the line spanned by P and Q. If you wanted to avoid homogenizations, you'd require λ+μ=1 but that usually leads to divisions I'd rather avoid until the very end.
Plug the resulting coordinates of R into the homogenized equation of the elliptic curve, and you obtain a homogeneous cubic equation in λ and μ, i.e. something like
αλ³ + βλ²μ + γλμ² + δμ³ = 0
with the α, β, γ and δ depending on your a, b, P, Q. For μ=0 you get a coordinate vector which is a multiple of P, and as homogeneous coordinates identify multiples, you get point P itself, which lies on the curve. So μ=0 must satisfy the equation, hence you know α=0 even before you compute it. Likewise λ=0 represents Q so δ=0 if that point lies on the curve. You are left with
(βλ + γμ)λμ = 0
The trailing two factors encode the two known intersections I just mentioned. The parenthesis is the third intersection, the one you need. Now simply pick λ=γ and μ=−β to obtain a simple expression for the third point of intersection.
If you want to dehomogenize at the end, simply divide the first two coordinates of the resulting homogeneous coordinate vector by the third.
If I didn't mess up my sympy computation, you have
β = 3*Px^2*Qx + 2*Px*Pz*Qz*a - Py^2*Qz - 2*Py*Pz*Qy + Pz^2*Qx*a + 3*Pz^2*Qz*b
γ = 3*Px*Qx^2 + 2*Pz*Qx*Qz*a - Pz*Qy^2 - 2*Py*Qy*Qz + Px*Qz^2*a + 3*Pz*Qz^2*b
Which is expectedly very symmetric in P and Q. So essentially you just need a single function, and then you get β=f(P,Q) and γ=f(Q,P).
In C++ and with the whole homogenization / dehomogenization in place:
inline double f(double Px, double Py, double Qx, double Qy, double a, double b) {
return 3*Px*Px*Qx + 2*Px*a - Py*Py - 2*Py*Qy + Qx*a + 3*b;
}
std::pair<double, double> third_intersection(double Px, double Py, double Qx, double Qy, double a, double b) {
double beta = f(Px, Py, Qx, Qy, a, b);
double gamma = f(Qx, Qy, Px, Py, a, b);
double denominator = gamma - beta; // Might be zero if line PQ is an asymptote!
double x = (gamma*Px - beta*Qx) / denominator;
double y = (gamma*Py - beta*Qy) / denominator;
return std::make_pair(x, y);
}
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
I am working with set of points, and my goal is to add 4 vectors together and calculate the resultant between start and end point,
Since I already prepared the code to do the above part which seems to be working fine but am not to sure about it.
Anyway the real reason I am posting this question is to do with the resultant direction and angle.
I find it hard to either understand the concept of finding resultant angle and direction as well as PROGRAMMING wise.
Consider this scenario....Image
1st add vectors "Head 2 Tail"
From what I have learned about vector addition is to subtract x2 - x1, y2 -y1 this will dive me the misplacement difference and do the same calculation for all the points from A-E
To get the resultant I square root all the points x to power of 2 and add all the y position to power of 2.
This ideology seem to work fine.....
But the QUESTION here is how do i get the angle and direction of that resultant....?
the code I use to calculate resultant:
double Pta;
double Ptb;
Point vect;
float R1, R2;
float resultant;
for(vector<Point>::iterator iter_a = Left_Arm_xy.begin()+1; iter_a != Left_Arm_xy.end(); ++iter_a)
{
if(center.y <= 240)
{
vect.x = iter_a->x - (iter_a -1)->x;
vect.y = iter_a->y - (iter_a -1)->y;
vect_add.push_back(Point(vect.x,vect.y));
for(vector<Point>::iterator iter_v = vect_add.begin(); iter_v - vect_add.begin() + 4 < vect_add.size(); iter_v+=4)
{
R1 = iter_v->x + (iter_v +1)->x + (iter_v +2)->x + (iter_v +3)->x;
R2 = iter_v->y + (iter_v +1)->y + (iter_v +2)->y + (iter_v +3)->y;
resultant = sqrt(pow(R1,2) + pow(R2,2));
}
}
Consider this..............
Ok lets consider Points A[2,4], B[4,8], C[10,12], To add this vectors i add vectors/points I subtract point B x4 - A x2 and point B y8 - A y4 and point C x10 - B x4 and point C y12 - B y8 this will give me the the displacements between points....Now to get the Resultant i add all the Points X's and Y's x's 2+4+10 = 16 y's 4+8+12 = 24, Next i would square root 16 ^2 + 24^2 = 28.84. So based on these calculations where resultant is a number not and x and y value how can i get the direction and angle....?
It is a simple summation of vectors.
(x, y) = (x1, y1) + (x2, y2) + ... = (x1+x2+..., y1+y2+...)
When you have the final vector, it's angle is found by using
tan(angle) = y/x
The angle between two vectors is generally defined as:
Angle = arccos( DotProduct(v1, v2) / ( Length(v1) * Length(v2) ) );
The direction is simply subtraction of the two vectors:
Direction = v2 - v1;
Usually, you have to normalize this to get a unit vector:
Len = SquareRoot( direction.x * direction.x + direction.y * direction.y );
Direction.x /= Len;
Direction.y /= Len;
Thus, you'll have a unit direction vector and the angle of the vector.
i have read section about The Extended Euclidean Algorithm & Modular Inverses,which states that it not only computes GCD(n,m) but also a and b such that a*n+b*b=1;
algorithm is described by by this way:
Write down n, m, and the two-vectors (1,0) and (0,1)
Divide the larger of the two numbers by the smaller - call this
quotient q
Subtract q times the smaller from the larger (ie reduce the larger
modulo the smaller)
(i have question here if we denote by q n/m,then n-q*m is not equal to 0?because q=n/m;(assume that n>m),so why it is necessary such kind of operation?
then 4 step
4.Subtract q times the vector corresponding to the smaller from the
vector corresponding to the larger
5.Repeat steps 2 through 4 until the result is zero
6.Publish the preceding result as gcd(n,m)
so my question for this problem also is how can i implement this steps in code?please help me,i dont know how start and from which point could i start to solve such problem,for clarify result ,it should look like this
An example of this algorithm is the following computation of 30^(-1)(mod 53);
53 30 (1,0) (0,1)
53-1*30=23 30 (1,0)-1*(0,1)=(1,-1) (0,1)
23 30-1*23=7 (1,-1) (0,1)-1*(1,-1)=(-1,2)
23-3*7=2 7 (1,-1)-3*(-1,2)=(4,-7) (-1,2)
2 7-3*2=1 (4,-7) (-1,2)-3*(4,7)=(-13,23)
2-2*1=0 1 (4,-7)-2*(-13,23)=(30,-53) (-13,23)
From this we see that gcd(30,53)=1 and, rearranging terms, we see that 1=-13*53+23*30,
so we conclude that 30^(-1)=23(mod 53).
The division is supposed to be integer division with truncation. The standard EA for gcd(a, b) with a <= b goes like this:
b = a * q0 + r0
a = r0 * q1 + r1
r0 = r1 * q2 + r2
...
r[N+1] = 0
Now rN is the desired GCD. Then you back-substitute:
r[N-1] = r[N] * q[N+1]
r[N-2] = r[N-1] * q[N] + r[N]
= (r[N] * q[N+1]) * q[N] + r[N]
= r[N] * (q[N+1] * q[N] + 1)
r[N-3] = r[N-2] * q[N-1] + r[N-1]
= ... <substitute> ...
Until you finally reach rN = m * a + n * b. The algorithm you describe keeps track of the backtracking data right away, so it's a bit more efficient.
If rN == gcd(a, b) == 1, then you have indeed found the multiplicative inverse of a modulo b, namely m: (a * m) % b == 1.
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
I am doing cropping a object in real time using c++. Here I know 2 coordinates of points A and B. I want to find 3rd point Z which is perpendicular to AB line. Z(x3,y3). It mean ABZ angle is 90. I gained 2 huge equations when I have used BZ slope and BZ distance. I have simplified it and gain following equation.
y3 ( y3 (k1 + y3) + k) = k ;
here k1 , k2 , k3 are Constance which has given. But k1 , k2 , k3 are not equals to each other.
I want to find y3 here. Please help me.
This sounds overly complicated, at least if I've understood your
problem. If there are no other constraints on Z (other than it be on a
line perpendicular to AB at B), then:
Z.x = B.x - (A.y - B.y);
Z.y = B.y + (A.x - B.x);
solves the problem. If there are additional constraints on Z, the above
expression still gives a point that, with B, defines the line.
Depending on the constraints, calculating the actual Z may be more or
less complicated, but it still shouldn't involve cubic equations.
If you do need to take a cube root, of course, the simplest solution is to use the standard function cbrt.
Generally, if you want to solve a cubic equation
x^3 + ax^2 + bx + c = 0
and you already have two solutions x1, x2 and are looking for the third solution x3
then you can do this by observing that
x^3 + ax^2 + bx + c = (x-x1)(x-x2)(x-x3),
and hence that
-a = x1 + x2 + x3
or
x3 = -a -x1 -x2
Hence finding the third solution of a cubic given the other two solutions is trivial.
The cubic equation has an exact solution. It's ugly but there you go.
Bear in mind that there will be pathalogical cases where numeric instabilities show up. If accuracy is important you should use more than one method.
This guy has solution code, and Dr. Math has some explanation.