Find N-th element in a sequence I found this programming challenge. I was wondering if it's possible to find a mathematical formula of the sequence, which, If I understood, is the following: A B C D E AA BB CC DD EE AAAA BBBB CCCC DDDD EEEE AAAAAAAA ... and I am to find the $N$-term.
I like the challenge but so far I couldn't work out a solution. I have searched a number of websites to look for similar problems, but have only found explanations on standard arithmetic, geometric, quadratic and cubic sequences. Would someone point me out good sources I could start with? Thanks
 A: Solution to your original programming challenge in python:
from math import log, ceil

names = ['Sheldon', 'Leonard', 'Penny', 'Rajesh', 'Howard']

def consumedDrinks(cycle):
    return len(names) * ((2 ** cycle) - 1)

def findCycle(drink):
    return ceil(log(drink / len(names) + 1, 2))

def lastCycleDrinks(drink):
    return drink - consumedDrinks(findCycle(drink) - 1)

def findLastDrinkPerson(drink):
    cycle = findCycle(drink)
    samePersons = 2 ** (cycle - 1)
    remainingDrinks = lastCycleDrinks(drink)
    index = (remainingDrinks - 1) // samePersons
    return names[index]

print(findLastDrinkPerson(1))
print(findLastDrinkPerson(6))
print(findLastDrinkPerson(1802))
print(findLastDrinkPerson(892656984))
print(findLastDrinkPerson(692293845123452346656984))

The program prints:
Sheldon
Sheldon
Penny
Leonard
Howard

Some explanations:

*

*Cycle: You have 5 persons initially, they will take their drinks in the first cycle. After that you have 10 persons and they will take their drinks in the second cycle. After that you have 20 persons and they will take their drinks in the third cycle etc.

*Function consumedDrinks(cycle): Returns the total number of consumed drinks when the given cycle ends (sum of geometric progression).

*Function findCycle(n): Returns the cycle to which the n-th drink belongs.

*Function lastCycleDrinks(n): Returns the number of drinks that will be consumed in the last cycle for a given number of drinks n.

*Function findLastDrinkPerson(n): Finds the name of the person that will drink the n-th drink (solution to the problem).

The idea is to find the number of cycles needed to drink the last drink. After that you can calculate how many drinks will remain in the last cycle. You can also calculate the number of same persons in the line in the last cycle (it doubles with every cycle). With all that it's easy to find the name of the person who drinks the last drink.
