I am trying to solve a Project Euler problem using Python 3 and I faced this problem. I want to find the nth element from this infinite series which the generator is producing where n = 1015 Here's the generator:
def test_gen():
yield 1
yield 1
sum_till_now = 1
while True:
sum_till_now += sum_of_digits(sum_till_now)
yield sum_till_now
The series is 1, 1, 2, 4, 8, 16, 23, 28,... and is produced fine by the generator
I tried the following approach:
def cal():
for index, item in enumerate(a):
if index == 1000000000000000:
print(item)
return
but it takes too much time to solve it. Is there any faster way to find the nth element of the generator when n is quite large? If my generator is inefficient do tell me. I don't want the correct answer to the problem if my approach itself is wrong. Thanks for the help.