Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static java.lang.Math.abs;
- public class SumFunc {
- private static final double EPS = 1e-6;
- public static void main(String[] args) {
- double V = 3;
- double a = -1;
- double b = 1;
- double step = 0.2;
- for (double x = a; x < b; x += step) {
- double[] func = func(V, x);
- System.out.printf("%f\t%f\t%d\n", x, func[0], (int) func[1]);
- }
- }
- public static double next(double cur, double c, double pow) {
- double next = cur * c * c / (pow * (pow - 1));
- return -1 * next;
- }
- public static double[] func(double V, double x) {
- double pow = 2;
- double cur = 1;
- double sum = cur;
- double next = next(cur, V * x, pow);
- int count = 1;
- while (abs(next) >= EPS) {
- count++;
- sum += next;
- pow += 2;
- cur = next;
- next = next(cur, V * x, pow);
- }
- return new double[]{sum, count};
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment