Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.6 kB
0
Indexable
Never
import java.util.Locale;
import java.util.Scanner;

public class Program {
	public static double abs(double value) {
		return (value < 0.0) ? -value : value;
	}

	public static double an(int n, double previous, double x) {
		return -previous * ((x * x) / n);
	}

	public static void calculateAndPrint(int n, double x, int e) {
		double tempSum = 1;
		double sumOfGreaterThanE = (1>abs(e))?1:0;
		double sumOfGreaterThanEOverTen = (1>abs(e)/10)?1:0;
		double mathResult = Math.pow(Math.E, -Math.pow(x, 2));
		double previous = 1;
		for (int i = 1; i < n; i++) {
			double an = an(i, previous, x);
			previous = an;
			tempSum += an;
			if (abs(an) > e) {
				sumOfGreaterThanE += an;
			}
			if (abs(an) > e / 10.0) {
				sumOfGreaterThanEOverTen += an;
			}
		}
		System.out.printf("1) Сумма %d слагаемых: %.8f %n", n, tempSum);
		System.out.printf("2) Сумма слагаемых больше %d (абс): %.8f %n", e, sumOfGreaterThanE);
		System.out.printf("3) Сумма слагаемых больше %.3f (абс): %.8f %n", e / 10.0, sumOfGreaterThanEOverTen);
		System.out.printf("4) Значение функции при помощи методов Math: %.8f", mathResult);
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.ROOT);
		Scanner scanner = new Scanner(System.in);
		System.out.print("Введите x: ");
		double x = scanner.nextDouble();
		System.out.print("Введите n: ");
		int n = scanner.nextInt();
		System.out.print("Введите e: ");
		int e = scanner.nextInt();
		calculateAndPrint(n, x, e);
	}
}