down vote

favorite

I wanted to create an app to display figurate numbers to some given value (x) according to this equation: (n+d-2)!*((p-2)(n-1)+d))/d!/(n-1)! = x where either d and p are given by the user. At first I did it with "int", but it displayed very litte numbers. Then I changed it to "long", but it still displayed not as much numbers as I wanted. So I decided I should use "BigInteger". Unfortunately, with BigInteger, the app doesn't work at all. It gets stuck, but it works, what I see in task manager (processor is loaded). Here is the code:

import java.math.BigInteger; import java.util.Scanner; public class main { public static void main (String[] args){ Scanner in = new Scanner(System.in); BigInteger W = BigInteger.valueOf(in.nextLong()); BigInteger K = BigInteger.valueOf(in.nextLong()); long x = in.nextLong(); BigInteger X = BigInteger.valueOf(x); for (int i = 1; i <= x; i++) { BigInteger I = BigInteger.valueOf(i); BigInteger J = BigInteger.ZERO; int poronanie = factorial(J.add(W).subtract(BigInteger.valueOf(2))).multiply((K.subtract(BigInteger.valueOf(2))).multiply(J.subtract(BigInteger.ONE)).add(W)).compareTo(I.multiply(factorial(W)).multiply(factorial(J.subtract(BigInteger.ONE)))); while (poronanie == -1){ J = J.add(BigInteger.ONE); poronanie = factorial(J.add(W).subtract(BigInteger.valueOf(2))).multiply((K.subtract(BigInteger.valueOf(2))).multiply(J.subtract(BigInteger.ONE)).add(W)).compareTo(I.multiply(factorial(W)).multiply(factorial(J.subtract(BigInteger.ONE)))); } if (poronanie == 0){ System.out.println(i); } } } public static BigInteger factorial(BigInteger n) { BigInteger result = BigInteger.ONE; while (!n.equals(BigInteger.ZERO)) { result = result.multiply(n); n = n.subtract(BigInteger.ONE); } return result; } }

Do You have any idea, why it doesn't work?