The final level of i’th position would be equal to total number of it’s divisors. So we need to find all the numbers which have exactly 5 divisors. The sequence comes off as 16,81,625,… which is A030514 - OEIS . The formula mentioned here is that the nth number of this sequence is the fourth power of nth prime number. So we store all the fourth powers of all the prime numbers . The answer for each n is just the number of elements in pre-calculated array which are lesser than or equal to n.