Simplify the conditional branches a bit and use long instead of int to store the max number.