Best algorithm to calculate factorial

8

So, I'm learning to develop in Java. The teacher asked me to develop a method for the Factorial. And also show what is happening on the screen, for example:

3! = 3 X 2 X 1 = 6

In fact, I made it happen, but is there any way for the code to get better? :

    int n = Integer.parseInt(txtN.getValue().toString());        
    int f = 1; 
    int c = n; 
    String s = ""; 

    while(c>=1){ 
        f *= c;
        if (c == 1) {
            s += c; 
        } else if (c>1) {
            s += c + " x " ;
        }
        c--;
    }

    lblFat.setText(s + " = " + Integer.toString(f));

The code generates the factorial and should also show the numbers on the screen.

Example

3! = 3 X 2 X 1 = 6
3! -> txtN (Local para o usuário inserir o número)
6 -> lblFat (local que gera o resultado)

Factorial in Java, do you have a better way to write code?

    
asked by anonymous 02.09.2016 / 00:35

4 answers

9

The most common is to separate the calculation from the presentation, but something simple so you can understand. According to what was put in the question I would do so (actually I would probably use a for ):

int f = 1; 
int c = n; 
String s = ""; 
while (c > 1) { 
    f *= c;
    s += c + " x " ;
    c--;
}
s += c;

See working on ideone .

To do recursively would be a lot more complicated, even if it is not needed .

Obviously using a simple integer there is a restricted maximum limit that can calculate the factorial. I believe that for this exercise this is not a problem, the intention is not something of production but rather to understand the operation of the algorithm, but it is worth knowing that in certain cases a BigInteger would be better, as commented by Bruno Bermann.     

02.09.2016 / 00:43
1

Dude you can do recursive.

Int FatorialRecursivo(int num){

   If(num==1) || (num==o)
      return 1;
   else
      return FatorialRecursivo(n-1)*n;
 }

Sorry for the messy code I'm answering for the APP, anything questions if you did not understand the recursion.

    
02.09.2016 / 02:07
1
public class calculeFatorial{
  public static void main(String[] args) {
    int fatorial = 1;

    for (int i = 1;i < 11 ; i ++ ) {
      fatorial *= i;

      System.out.println("Fatorial de " +i+"=" +fatorial);
    }
  }
}
    
11.08.2017 / 03:51
0

Introducing Factorials with for:

long fat=1;
String mult="";
for (int i=5; i>1; i--){
    fat *=i;
    mult +=i+" x ";
}
System.out.print(mult+"1 = "+fat);

See rolling on Ideone

It is more advisable that the variable that accumulates the values is of type long, considering that the int has greater limitation of spectrum. To change the value, simply change the 5 by a variable and choose how to feed it.

At the end, 1 was added outside of the repeat structure. Because it is a factor, the multiplication is not affected by it, it can be removed, saving a lap in the calculations, only being added to the string that had already received the "X" after the 2.     

17.04.2017 / 01:18