Welcome to the Ranch
You didn’t use the
code button correctly, but I have sorted that out. I also got rid of lots of unnecessary tags and excess whitespace, which makes the code more difficult to read.
Wny have you got comments like
// BigInt for n, rather than simply calling the variable n? You also have too many imports. Isn’t there a BigInteger.ONE object ready for you to use?
You cannot work out the algorithm by reading the code. You need to write the algorithm on paper, and then carefully translate that to code. Have you done that? That is probably the only way you can sort out the problem.