A little context:
Book has 411 Pages
Read a random number of pages on the first day which is unknown
Number of pages to read next day is the square of the sum of the digits of the page currently on
I.e. on Page 36: read 81 pages next day
On day 6 The square answer directly adds to current page number to make 411
How many pages did she read on the first day and how many pages did she read on the 6 days?
This is what I have so far:
from itertools import accumulate, count def read_book( start ): for i in range( 6 ): yield start; start += sum( int( digit ) for digit in str( start ) ) ** 2 def find_start( pages ): for start in count( 1 ): for read in read_book( start ): pass if read == pages: return start previous_answer, start = 0, find_start( 411 ) for i, read in enumerate( read_book( start ), 1 ): print( "Pages read on day", i ,"was", read-previous_answer ,"\t(Now on page", str(read) + ")\n") previous_answer += read-previous_answer
Pages read on day 1 was 61 (Now on page 61)
Pages read on day 2 was 49 (Now on page 110)
Pages read on day 3 was 4 (Now on page 114)
Pages read on day 4 was 36 (Now on page 150)
Pages read on day 5 was 36 (Now on page 186)
Pages read on day 6 was 225 (Now on page 411)
Is there a more efficient way of writing this using fewer lines of code?