解决给定系列的算法是什么?

时间:2016-10-21 09:36:12

标签: java algorithm

计算给定系列的算法是什么?

Series to be calculated

这是我现在编码的内容:

public static double sumOfSeries(double x, int numTerms) { // -1 <= x <= 1
        double sum = x;
        for(int i = 1; i <= numTerms; i++) {

        }       
        return sum;
    }

2 个答案:

答案 0 :(得分:0)

必须找到所提方程中的所有规律性。

x - &gt; x ^ 3 - &gt; x ^ 5 - &gt; x ^ 7 - &gt;

然后在循环之前制作单独的变量N1 = x,在每次迭代结束时制作N1 * = x * 2

1 - &gt; 1 - &gt; 1x3 - &gt; 1x3x5 - &gt; 1x3x5x7 - &gt;

在lopp之前分离变量N2 = 1并且在每次迭代结束时将N2 * =(i * 2-1)分开

等等

最后,你会在每次迭代开始时将所有这些部分组合在一起,如sum + = N1 * N2 * N3 /(N4 * N5)

答案 1 :(得分:0)

尝试将系数与多项式分开,并用不同的方法计算。例如:

        public class NewClass {

        public static void main(String[] args) {
             int greatestExponent = 9;
             double x = 0.5;
             System.out.println(calculate(x,greatestExponent));
        }
        public static double getCoefficient(int n){
            double coff = 1;
            for(int i=1; i<n; i++){
                if(i%2==0){
                    coff = coff/i;       //if even put it in the denominator
                }
                else{
                    coff = coff*i;       // else numerator 
                }
            }
            return coff/n;          // at the end divide with the exponent (ex. x^9 /9)
        }
        public static double calculate(double x, int expo){
            double result = 1;
            for (int i = 1; i<= expo; i+=2){
                result +=  (getCoefficient(i)*Math.pow(x, i)); //for each odd exponent calculate the cofficient and x^i
            }
            return result;
        }
    }