factorial of a number without overflowing the long variable?

2

I have a code that performs permutations, and at first seems to work, the problem is that when you enter a number greater than 20 the results are erroneous and as I read and it is because they leave so many result characters that are not possible to store them all in the variable of type long, my question is whether it is possible to avoid overflowing the variable long or if there is a variable that allows to store such a large number.

I would greatly appreciate your help, I also accept suggestions if you see something wrong in my code.

thanks.

try {
        int vn = Integer.parseInt(tn.getText());
        int vr = Integer.parseInt(tr.getText());
        long rvn = vn;
        int rr = vn - vr;
        long rvr = rr;
        long resultado;
        if (rr == 0 || rr == 1) {
            rr = 1;
            for (int i = 1; i < vn; i++) {
                rvn = rvn * i;
            }
            resultado = rvn / rr;
            tre.setText(String.valueOf(resultado));
        } else {
            for (int i = 1; i < vn; i++) {
                rvn = rvn * i;
            }
            for (int i = 1; i < rr; i++) {
                rvr = rvr * i;
            }
            resultado = rvn / rvr;
            tre.setText(String.valueOf(resultado));
        }
        tr.setText(null);
        tn.setText(null);
    } catch (NumberFormatException as) {
        JOptionPane.showMessageDialog(null, "Solo Puedes Ingresar Numeros", "Error", ERROR_MESSAGE);
    }
    
asked by Diego Montiel 01.08.2017 в 09:59
source

2 answers

2

You can use the BigIntegrer class defined in the java.math package.

import java.math.BigInteger;

If you want information on how to use it, here is a tutorial (in English) link

    
answered by 01.08.2017 / 10:10
source
1

What I did to make the doubt I had, was to create a BigInteeger type method, where what is done is to take the factorial of a number, as I said above, with the method that had the variable of type long is devoured , but reading a bit eh implemented the class Math with the type of varibale BigInteger and this way I get the correct value without limitation.

 public static BigInteger factorial(BigInteger n) {
//por definicion el factorial de 0 y de 1 es 1.
    if(n.equals(BigInteger.ONE)||n.equals(BigInteger.ZERO)) {
        return BigInteger.ONE;
    }
    //si no es 1 o 0 hace la operación.
    return n.multiply(factorial(n.subtract(BigInteger.ONE)));
}

already having the recursive method that would allow the factorial to be taken out of any number without restriction, since I am left to carry out the corresponding operations with those results and I am like that.

try {
        BigInteger vn = new BigInteger(tn.getText());
        BigInteger vr = new BigInteger(tr.getText());
        BigInteger rr = vn.subtract(vr);
        BigInteger rfn = factorial(vn);
        BigInteger rfr = factorial(vr);
        BigInteger rfrr = factorial(rr);
        BigInteger rrr = rfr.multiply(rfrr);
        BigInteger resultado = rfn.divide(rrr);
        tre.setText(String.valueOf(resultado));
        tn.setText(null);
        tr.setText(null);
    } catch (NumberFormatException r) {
        JOptionPane.showMessageDialog(null, "Solo Puedes Ingresar Numeros", "Error", ERROR_MESSAGE);
    }
    
answered by 02.08.2017 в 03:34