Operation with Java Factorials

Asked

Viewed 129 times

4

Can anyone ask this question in Java? I have no idea how to do, I’m still beginner.

inserir a descrição da imagem aqui

Input format

An integer x corresponding to the X of the equation and an integer n indicating a number of series terms, input ends when x = 0 and n = 0.

Output format

A floating point number formatted with six decimal places.

Examples of: Entree

2 5 2 4 4 5 0 0

Exit

1.454674 1.453968 2.834568

  • Input Examples: 2 5 --> output: 1.454674

  • What have you tried so far? Factorials are a very simple concept for the program. You really need to break this question into functions.

2 answers

6

Being T one of the terms of the series in question, given in general by:

T = SINAL * (X ^ EXPOENTE)
            --------------
               DIVIDOR!

And whereas:

  • SINAL is a number that alternates between values 1 or -1, beginning with -1
  • EXPOENTE is a sequence that begins with 2 and increasing of 2 in 2 until the operation is completed (2, 4, 6, ..., N)
  • DIVISOR is a sequence that begins with 3 and increasing of 2 in 2 until the operation is completed (3, 5, 7, ..., N)

And yet:

  • S is the value of the current sum and its initial value is X
  • S_ANTERIOR is the value of the previous iteration
  • X is the value entered by the user

Do the following:

  1. Declare variables to control the above values
  2. Initialize variables with the initial value of each of them
  3. Make a loop that:
    1. Calculate the value of T as in the above function
    2. Save the value of S in S_ANTERIOR - S_ANTERIOR = S
    3. Some S with T - S = S + T
    4. Check the accuracy of the result.
      This can be done by checking whether S - S_ANTERIOR < 0.000001, because it means that all other calculated terms will not affect the first six decimal places of the sum.
      • If the condition is true, end the loop.
      • If the condition is false, calculate the next values of SINAL, EXPOENTE and DIVISOR and return to the beginning of the loop (item 3.1)

The hardest thing for those who are starting programming is not programming, but understanding the problem first.

What I did above was describe the implementation logic in terms of an algorithm.

Whenever you have a problem at hand, the first thing you need to understand is how the problem is solved in algorithm format, which is nothing more than a set of steps to get to the result in general deterministic or iterative way.

Once you understand how the problem is solved, here comes the part of translating this into a program.

You will probably encounter specific difficulties in the course of implementation, but once you know what you want, it is easy to find the answer.

How to receive the initial value, how to declare the variables and perform the mathematical operations, all these are questions that you can easily now find the answer, after all know exactly which question to ask.

  • 2

    Very didactic. A good example of how one should/can answer this type of questions.

  • 1

    @utluiz EXPOENTE would not be increased by 2 in 2?

  • @hlucasfranca that same, already updated the answer

0

That would be about it:

public static int Fatorial(int num)  
{  
    if (num != 1) {  
        num *= Fatorial(num - 1);
    }  
   return num;  
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.