Enigmatic Code

Programming Enigma Puzzles

Enigma 1122: Chapter and worse

From New Scientist #2278, 17th February 2001 [link]

While waiting for a much-delayed train, George found himself trying to read a very boring book. He soon gave up and started counting its pages instead. Chapter 1 started on Page 1, and each subsequent chapter started at the top of a page. The boredom factor was enhanced by the fact that the length in pages of each chapter was equal to the chapter number multiplied by the length of Chapter 1.

With still no sign of the train, George proceeded to total all the page numbers in each chapter. Again, the totals for each chapter were exact multiples of the total for Chapter 1, but this time the multiples did not equate to chapter numbers. For the last chapter the multiple was a prime number, even though the chapter number was not.

How many pages were there in the book?

[enigma1122]

Advertisements

2 responses to “Enigma 1122: Chapter and worse

  1. Jim Randell 26 March 2017 at 8:55 am

    This Python program considers possible values for the number of pages in the first chapter (between 1 and 50 pages), and the total number of chapters in the book (up to 100 chapters). It runs in 47ms.

    from enigma import irange, is_prime, printf
    
    # if chapter 1 has n pages, then the chapter 2 has 2n pages, chapter 3
    # has 3n pages, etc.
    #
    # chapter 1: page 1 - page n
    # chapter 2: page (n + 1) - page (3n)
    # chapter 3: page (3n + 1) - page (6n)
    # chapter 4: page (6n + 1) - page (10n)
    # ...
    # chapter k: page (T(k - 1)n + 1) - page(T(k)n)
    
    # n = number of pages in chapter 1
    # m = max number of chapters to consider
    def solve(n, m):
      (end, s1) = (0, 0)
      # consider chapters
      for k in irange(1, m):
        # the start and end pages
        start = end + 1
        end += k * n
        # the sum of the pages in the chapter
        s = sum(irange(start, end))
        if k == 1:
          # record the sum for the first chapter
          s1 = s
        else:
          # check the sum is a multiple of the first chapter
          (d, r) = divmod(s, s1)
          # each sum must be an exact multiple
          if r != 0: return
          # the last chapter is not prime, but the multiple is
          if not(is_prime(k)) and is_prime(d):
            yield (k, end)
        
    # consider the number of pages in chapter 1
    for n in irange(1, 50):
      for (k, end) in solve(n, 100):
        printf("{end} pages [{k} chapters, chapter 1 has {n} pages]")
    

    Solution: There are 105 pages in the book.

    The book has 6 chapters.

    Chapter 1 has 5 pages, pages 1 – 5, with a sum of 15.
    Chapter 2 has 10 pages, pages 6 – 15, with a sum of 105, this is exactly 7 times the sum of Chapter 1.
    Chapter 3 has 15 pages, pages 16 – 30, with a sum of 345, this is exactly 23 times the sum of Chapter 1.
    Chapter 4 has 20 pages, pages 31 – 50, with a sum of 810, this is exactly 54 times the sum of Chapter 1.
    Chapter 5 has 25 pages, pages 51 – 75, with a sum of 1575, this is exactly 105 times the sum of Chapter 1.
    Chapter 6 has 30 pages, pages 76 – 105, with a sum of 2715, this is exactly 181 times the sum of Chapter 1.

    The final multiple, 181, is a prime number and the chapter number, 6, is not.

  2. Hugh Casement 26 March 2017 at 12:22 pm

    The sum of the page numbers in chapter k is k(nk² + 1)/(n + 1) times T(n).
    In trying to work out why that should be, I got a bit bogged down in tetrahedral numbers,
    or perhaps that should be mth triangular numbers where m is itself a triangular number.
    Somehow I seem to have lost an hour’s sleep!

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: