add recursive func to calculate index.

edited on mobile phone.