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);
}