Horner.java
public class Horner
{
public static double eval(double x,
double[] p)
{
double result = 0;
for (int i = p.length - 1; i >= 0;
i--)
result = p[i] + (x * result);
return result;
}
public static void main(String[] args)
{
int N =
Integer.parseInt(args[0]);
double[] p = new double[N];
p[0] = 1;
for (int i = 1; i < N; i++)
{
p[i] = p[i-1] / i;
}
while (!StdIn.isEmpty())
{
double x = StdIn.readDouble();
StdOut.println(eval(x, p));
StdOut.println(Math.exp(x));
StdOut.println();
}
}
}
No comments:
Post a Comment