M
M
muzclubs2016-11-30 11:18:40
Java
muzclubs, 2016-11-30 11:18:40

Why does the factorial go to zero?

Good afternoon.
It is necessary to calculate the expansion function in a series e^(-x^2+1).
It is known that e^x= (x^0)/(0!) + (x^1)/(1!) + (x^2)/(2!).. Do this until the value of the sum at the i-th step will be greater than 0.25 in absolute value. At some point, the factorial becomes equal to 0 for me. Please tell me, what is the problem?

import java.util.Scanner;

class V17{
  public static void main(String[] args){
    final double E = 0.25;
    int n = 5; // Число шагов
    double[] x = {-2.7, -0.1, 2.9, 17.9, 117.0};
    
    /*Scanner in = new Scanner(System.in)
    int n = in.nextInt();
    double[] x = new double[n];
    for(int k=0; k<x.length; k++){
      System.out.print("x" + k + " = ");
      x[k] = in.nextInt();
    }*/
    
    double[] answer = new double[n];
    double[] argStep = new double[x.length];
    for(int k=0; k<argStep.length; k++){
      argStep[k] = -1*Math.pow(x[k],2)+1;
    }
    
    double e, s, g;
    int fact, factK;
    for(int k=0; k<n; k++){
      s = 1; // Сумма шага
      e = 1; // Сумма общая
      g = 1; // x^i
        fact = 1; 
            factK = 0;	
      while(Math.abs(s)>E){
        g = g*argStep[k];
        factK++;
          fact=fact*factK;
        s = g/fact;
        System.out.println(fact);
        e = e + s;
      }
      answer[k] = e;
    }
    for(int k=0; k<answer.length; k++){
      System.out.println("e^(" + argStep[k] + ") = " + answer[k]);
    }
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2016-11-30
@muzclubs

You can determine this yourself with step-by-step debugging:
So you can find the moment at which the value becomes incorrect, and determine the reasons that led to this.

A
abcd0x00, 2016-12-03
@abcd0x00

These problems are solved not through factorial, but through recurrence relations. You just fell into the rookie trap. They see that there is a factorial and begin to solve through the factorial.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question