I'm trying to code (whole day)number generation system, it starts with 3 and the value decreases by 1 until it reaches 1. Once it reaches 1, it resets its number to a new starting value 2x as that of the first number. And then with each new reset 2x as the first number of the earlier reset. Like shown in bellow,
3 2 1 6 5 4 3 2 1 12 11 10 9 8 7 6 5 4 3 2 1...
The program should return the value of the nth position that user request.
user can enter n < (10^12)
Tried skipping some steps if the target position is far away from the current position of the loop
# requesting position
tt = int(input())
# starting t
t = 1
# starting value
sv = 3
# current value
cv = 3
while (True):
# check if we have reached target time
if (t == tt):
# print the current value
print("{}".format(cv))
break
# if there's a loong way to go, skip some
if (t + cv < tt): # starting_t+current_value<target_t
t += cv
sv = sv * 2
cv = sv
if cv % 2 == 0:
if (t + cv // 2 < tt):
t += cv // 2
cv = cv // 2
continue
continue
# check if value is 1 and double it
if (cv == 1):
# set new starting value
sv = sv * 2
# set new current value
cv = sv
# elapse time
t += 1
continue
# elapsed time
t += 1
# changed value
cv -= 1
Currently, this program takes more than 2 minutes to return the result for n>10^10. I need to reduce the time taken for the process as much as possible. What can I do to reduce the time taken for the process? (expect to reduce it to a couple of seconds) any reference may be helpful