fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner scanIn = new Scanner(System.in);
  13. double x, e;
  14.  
  15. x = scanIn.nextDouble();
  16. e = scanIn.nextDouble();
  17.  
  18. double sum1 = x; // в случае, когда i=0
  19. double sum2 = -x * x * x / 6; // в случае, когда i=1
  20.  
  21. for (int i = 2; Math.abs(1 / sum2 - 1 / sum1) >= e; i++) {
  22. sum1 = sum2;
  23. sum2 *= -x * x / ((2 * i) * (2 * i + 1));
  24. }
  25.  
  26. System.out.println("1 / sum2: " + 1 / sum2);
  27. }
  28. }
Success #stdin #stdout 0.11s 2841600KB
stdin
-12
0.6
stdout
1 / sum2: 0.003472222222222222