Define the sequences an and bn as follows: a1=2017 and b1=1.
For n>1, if there is a greatest integer k>1 such that an is a perfect kth power, then an+1=kan, otherwise an+1=an+bn. If an+1≥an then bn+1=bn, otherwise bn+1=bn+1. Find a2017.