# Enigmatic Code

Programming Enigma Puzzles

## Enigma 660: Bookworm

From New Scientist #1815, 4th April 1992 [link]

I have ten volumes of Applications of Abstract Algebra together in order on my shelves. Each volume has 100 pages and the page numbering continues through the volumes from 1 to 1000.

A bookworm started to nibble through the even-numbered side of a leaf (whose number was a perfect square): it kept nibbling in a straight line — through covers as well — and when it stopped it had just emerged through a leaf onto its odd-numbered side (whose number was also a perfect square). Ignoring the covers, it had eaten through a number of leaves which was also a perfect square.

What were those three squares?

[enigma660]

### One response to “Enigma 660: Bookworm”

1. Jim Randell 20 May 2022 at 12:55 pm

The key thing to note with this kind of puzzle is that when books are placed on shelves, the spine is usually placed facing out, so that the pages of a book numbered 1/2, 3/4, 5/6, …, 99/100 when placed on the shelf would be positioned (left to right): 100/99, 98/97, …, 2/1.

So if all 10 volumes are stacked left (vol 1) to right (vol 10), the pages will go:

[100/99 … 2/1][200/199 … 102/101][300/299 … 202/201] … [1000/999 … 902/901]

The bookworm starts on an even page number and eats through leaves from left right and ends on an odd number in a different volume.

This Python program runs in 60ms. (Internal run time is 311µs).

Run: [ @replit ]

```from enigma import (irange, div, is_square_p, printf)

# consider starting leaf (even numbered squares)
for i in irange(2, 30, step=2):
start = i * i
(sv, sp) = divmod(start - 1, 100)

# consider finishing leaf (odd numbered squares)
for j in irange(i + 1, 31, step=2):
finish = j * j
(fv, fp) = divmod(finish - 1, 100)
# worm eats left to right (and ends in a different volume)
if not(sv < fv): continue

# calculate the number of leaves eaten (must be a square)
n = 50 * (fv - sv - 1) + div(sp + 1, 2) + div(100 - fp, 2)
if not is_square_p(n): continue

# output solution
printf("start={start} finish={finish} n={n}")
```

Solution: The three squares are: 16 (start page), 289 (finish page), 64 (number of leaves chomped).

This site uses Akismet to reduce spam. Learn how your comment data is processed.