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 6 years ago.
Improve this question
A line is given and a bunch of points are given. I have to find a point on line for which sum of distances from given points is minimum. I could't find any algorithm to implement in c. Please help, thanks in advance.
Without loss of generality, the line is the X axis (otherwise rotate the whole geometry). Then you want to minimize
Sum √[(X - Xk)² + Yk²]
which you can do by canceling the first derivative
Sum (X - Xk)/√[(X - Xk)² + Yk²] = 0
Unfortunately, this is a nonlinear equation that will require numerical methods.
As a starting approximation, you can use the minimizer of the sum of the squared distances,
Sum [(X - Xk)² + Yk²]
by solving
Sum (X - Xk) = 0
which simply gives the point (X*, 0) where X* is the average abscissa.
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 3 years ago.
Improve this question
I wish to write a C code where I can find the position of a point on a line between two points in a 2D coordinate grid. The two points positions I know, as well as each of the points' distances to the new desired point. However, I do not wish to use trig functions as that proves to be more expensive than I would like. I have heard of a simple solution using 1D interpolation, but I am unfamiliar with this solution. Could someone shed light on this? Thanks!
Assuming the following:
p1 is the first point and has coordinates (p1.x, p1.y);
p2 is the second point and has coordinates (p2.x, p2.y);
pi is the interpolated point on the line connecting p1 and p2;
d1i is the given distance from p1 to pi;
di2 is the given distance from pi to p2.
Let d12 be the distance from p1 to p2. Then: d12 = d1i + di2.
(Alternatively, d12 = sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y)) via the theorem of Pythagorus.)
Let ratio = d1i / d12.
Then:
pi.x = p1.x + ratio * (p2.x - p1.x)
pi.y = p1.y + ratio * (p2.y - p1.y)
This also works if d1i and di2 are given as signed distances, where a positive distance is interpreted as "in the direction from p1 to p2" and a negative distance is in the opposite direction.
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 6 years ago.
Improve this question
I'm having trouble on thinking of away to attack this problem.
X is defined below. For n=1,x=0.5,n=2,x=0.833.As you add more terms, X increases. Calculate n for which X becomes larger than 4. First write the algorithm and then implement the code in C.
x= 1/2+1/3+...1/n+1 answer: n = 83
The only thing I'm sure of is that it uses a for loop.At first I was thinking something like
For(int i = 0; i <= n.....
That doesn't seem close though.I dunno..Can I get a hint on where to start?
You will obviously compute the partial sums X.n and stop when X.n<4 and X.n+1>4.
To compute the partial sums, keep an accumulator variable and add the fractions one after the other
n= 0
S= 0
// Repeat the following instructions
n+= 1
S+= 1/(n+1) // Now, S = X.n
Remains to find the stopping condition. As the value of S goes increasing from 0, we will stop as soon as S exceeds 4. In other words, continue as long as S remains below 4.
n= 0
S= 0
while S < 4
n+= 1
S+= 1/(n+1) // Now, S = X.n
Translate that to C syntax.
Remains to look closer at the possibility that X.n = 4.
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 7 years ago.
Improve this question
if the range of d is 1<= d <= 10^101 and n is 1<= n <= 200. since the range of double is 2.3E-308 to 1.7E+308. when i take input 11111111111111111111 as d then the value d become 11111111111111111000.000 when i show the value to terminal. that means that it couldn't take the input correctly then how will it give correct value for 10^101. i need know the nth root of d. d will be always in form of p = k^n. that's why i added pow function to know the nth root. but the problem is that the range of p is huge. what i am trying is to solve this problem Power of Cryptography !
int main(){
double d,n;
scanf("%lf%lf", &n, &d))
{
printf("%lf\n", pow(d, 1/n));
}
return 0;
}
A double precision number is not capable of holding all the values between 2.3E-308 to 1.7E+308, it is capable of holding a value between these numbers to a precision of about 15 decimal places.
That means some numbers (such as your example) require more precision than the 8 bytes of data can store.
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 7 years ago.
Improve this question
c program to print minimum number of days required to spread rumour in matrix to all houses located like a matrix of m*n. It takes exactly one day to travel rumour from 1 house to other rumour travels only horizontally or vertically
Here is code i have written but it is not getting accepted might be wrong at higher inputs.1<=m<=100000 and 1<=n<=100000.
here is my code
#include<stdio.h>
int main()
{
long m,n,days;
scanf("%ld",&m);
scanf("%ld",&n);
if(m<1||m>1000000||n<1||n>1000000)
return 0;
if(m==2&&n==2)
printf("3");
else
if((m+n)%2==0)
days=(m+n)/2;
else
days=(m+n)/2+1;
printf("%ld",days);
return 0;
}
The fastest way that the rumour will travel is when the roumor starts in the middle of the matrix.
Since it travels only horizontally or vertically, if it starts from the middle then it will take m/2 + n/2 days to reach a corner in the worst case.
Just try this printf("%d\n",m/2+n/2); it should work.
NOTE: Add 1 if you have to include a day for the starting house.
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 8 years ago.
Improve this question
Today I came across a question. It says that:
You need to find if a number can be expressed as sum of two perfect powers. That is, given x find if there exists non negative integers a, b, m, n such that a^m + b^n = x where 1 <= x <= 1000000 and m > 1, n > 1
Could someone please explain me how this can be done?
I know that we can write something like this:
for(int a = 1; true; a++){
for(int b = 1; true; b++){
// And so on and so forth
}
}
But this is not the very efficient (or correct) way of doing so.
Thanks.
Sometimes, that is the only way to solve these kind of problems. The one you have posted belongs to a class of problems called "one way functions": there is a trivial implementation of the problem in one way...
Given four non negative integers, a, b, m and n, find x so a^m + b^n = x
But the other way...
Given x, find four non negative integers a, b, m and n, so a^m + b^n = x
is non trivial. In fact, it could be impossible to solve it, or your best chance, have to use a force brute algorithm to solve it, which is what you have proposed.