It would be helpful if you provided more details on what exactly you are stuck on and need help with. I'm not sure how useful it is if you write a problem like this and somebody gives you the code which you can copy paste.
How would you approach this problem? You are a Java beginner so let's take Java out of the picture for a moment. You need to be able to come up with a plan in plain English and then convert your solution into Java code.
If you have no idea how to start, write down the output for the first 20/25 values. Check if you can notice a pattern and go on from there.