please answer this 2 question in python language of computer science
it is urgent
please give me proper answer
i will mark you brilliant
Share
please answer this 2 question in python language of computer science
it is urgent
please give me proper answer
i will mark you brilliant
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Answer:
ans In c
Explanation:
// C implemenation to find the sum
// of series 1 + x^2 + x^3 + ....+ x^n
#include <math.h>
#include <stdio.h>
// Function to print the sum
// of the series
double sum(int x, int n)
{
double i, total = 1.0, multi = x;
// First Term of series
printf("1 ");
// Loop to find the N
// terms of the series
for (i = 1; i < n; i++) {
total = total + multi;
printf("%.1f ", multi);
multi = multi * x;
}
printf("\n");
return total;
}
// Driver Code
int main()
{
int x = 2;
int n = 5;
printf("%.2f", sum(x, n));
return 0;
}
2)We assume that you know enough never to evaluate a polynomial this way:
p=c[0]+c[1]*x+c[2]*x*x+c[3]*x*x*x+c[4]*x*x*x*x;
or (even worse!),
p=c[0]+c[1]*x+c[2]*pow(x,2.0)+c[3]*pow(x,3.0)+c[4]*pow(x,4.0);
Come the (computer) revolution, all persons found guilty of such criminal behavior will be summarily executed, and their programs won’t be!
- W.H. Press et al, Numerical Recipes in C, 2nd ed (1988)
This is the best general-purpose way to do it, assuming that you want double-precision floating point:
double evaluate(double x)
{
return 1 + x * (-1 + x * (1 + x * (-1 + x * 1)));
}
That last multiply-by-one is of course redundant and will be optimised out, but you can remove it just to be sure if you want. I left it in for clarity.
In general, to evaluate a0+a1x+a2x2+a3x3+a4x4 :
double evaluate(double x)
{
return a0 + x * (a1 + x * (a2 + x * (a3 + x * a4)));
}
This is known as Horner's method or Horner’s rule, and it should be in every programmer’s bag of tricks.
There are a few advantages to doing it this way:
It uses the minimum number of multiplications, which are the most computationally expensive operation here. (Though they’re not that expensive actually.)
It usually a very numerically stable method; this depends on the coefficients in general, but it’s true here. More on this later.
There is only one possibly better way to do it, and that is to use the fused multiply-add operation:
#include <math.h>
double evaluate(double x)
{
return fma(x, fma(x, fma(x, fma(x, a4, a3), a2), a1), a0);
}
The function fma(x,y,z) calculates x*y+z using the best operation available to your CPU.
FMA is an extremely important operation (partly because this is how we evaluate polynomials, and evaluating polynomials is an extremely common operation) that many modern CPUs have a special instruction which performs it much faster and slightly more accurately than a separate multiplication and addition. You can find out if your implementation will do this by testing to see if the FP_FAST_FMA macro is set. If it isn’t, it will just do multiply and add separately.
Here are some things to watch for:
Don’t use pow. It is a very expensive and less-accurate operation than a modest number of multiplications. In general, it’s probably a mistake to use pow unless you’re raising a number to a large or non-integer power, and four is not large. I write a lot of number-crunching code at the moment, and I don’t think I’ve used pow in the last decade.
If you are adding up the terms one at a time, add the smallest ones first. So, for example, if x is likely to be small, this:
a0 + (x * a1 + (x*x*a2 + (x*x*x*a3 + x*x*x*x*a4)))
is usually much better than this:
(((a0 + x * a1) + x*x*a2) + x*x*x*a3) + x*x*x*x*a4
The reason why is that this minimises round-off error. But if x is large, the second one is better for the same reason. Using Horner’s rule avoids the problem entirely.
Well, not entirely. For your coefficients, this is still going to have issues if x is very close to 1, because then you’re subtracting numbers that are almost equal. But if x is very close to 1, the real answer is unstable. I’ll leave that issue for now.
It’s interesting that such a simple function can have complicated implications, but that’s numeric analysis for you.