### Random Post

### Recent Posts

- Puzzle 50: Football and addition
- Enigma 1066: Members of the clubs
- Enigma 443: The bells they are a-changing
- Tantalizer 455: Ballistico
- Tantalizer 456: Square deal
- Enigma 1067: Bye!
- Enigma 442b: Oh yes I did! Oh no you didn’t!
- Puzzle 51: A multiplication
- Enigma 1068: Triangular Fibonacci squares
- Enigma 442a: Hark the herald angels sing

### Recent Comments

### Archives

### Categories

- article (11)
- enigma (1,165)
- misc (2)
- project euler (2)
- puzzle (42)
- site news (45)
- tantalizer (45)
- teaser (3)

### Site Stats

- 180,250 hits

Advertisements

The following Python program runs in 37ms.

It accumulates the possibilities in various ways and eliminates options that don’t match the conditions of the problem, until we are left with the solution.

Solution:Amy’s number is 6. Bob’s number is 7. Carol’s number is 8.Great stuff. I just wonder in line 5, if the range is 1-9, I was calculating on one positive digit, as in the first paragraph of the question.

In Python the

range(a, b)function generates numbers fromatobbutnotincluding the endpoint. Sorange(1, 10)produces[1, 2, 3, 4, 5, 6, 7, 8, 9]. It’s handy when you’re indexing into an array of length n – you can just userange(n)– but at other times it can be annoying!This is one of the rare cases in which Perl is more readable than Python. The equivalent Perl expression is simply

1..9.In fact I’ve introduced an

irange()(inclusive range) function to theenigma.pymodule precisely for when you want an iterator that includes both its endpoints. If you use that you can useirange(1, 9)to get[1, 2, 3, 4, 5, 6, 7, 8, 9].