Java program to solve the mathematical sequence problem