# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1672: Heptagony

From New Scientist #2838, 12th November 2011 [link]

I was surfing the internet recently, and found a reference to polygonal numbers. These are series such as the triangular numbers, pentagonal numbers and others, including the series of heptagonal numbers, which starts 1, 7, 18, 34… I asked my nephew to tell me the next member of this series, which he said was 55. Later he told me that he had found a set of six consecutive heptagonal numbers, all less than two million, where the difference between the first and last was divisible by all of the digits 1 to 9. One of the intermediate heptagonal numbers in this set was divisible by just four of these digits.

Note: As originally published this Enigma asked for a set of five consecutive heptagonal numbers, which admits no solution. The magazine later issued this corrected puzzle.

[enigma1672]

### 2 responses to “Enigma 1672: Heptagony”

1. Jim Randell 30 November 2011 at 5:00 pm

A quick trip to Wikipedia reveals that for formula for a Heptagonal Number is (5n² – 3n)/2.

You can then make a rolling list of six consecutive heptagonal numbers until you find the ones that satisfy the conditions. This Python program runs in 29ms.

```from itertools import count

# count how many of the digits 1 to 9 exactly divide n
def check(n):
return sum(1 for d in range(1, 10) if n % d == 0)

# consider k-sequences of consecutive hexagonal numbers
(k, hs) = (6, [])
for n in count(1):
h = (n * (5 * n -  3)) // 2
if not(h < 2000000): break
hs.append(h)
if len(hs) > k: hs.pop(0)
if len(hs) < k: continue
# check the difference between the first and last number is divisible by all 9 digits
d = hs[-1] - hs[0]
if check(d) != 9: continue
# find intermediates divisible by only 4 digits
xs = list(filter((lambda x: check(x) == 4), hs[1:-1]))
if len(xs) == 1:
print(xs[0], hs, d)
```

Solution: The intermediate heptagonal number is 409455.

2. Hugh Casement 12 March 2016 at 10:21 am

The difference between the nth and (n + 5)th heptagonal numbers is 25n + 55, so is always an integer multiple of 5.  If n = 9k + 5 it is also a multiple of 9, if 8k + 1 of 8, if 7k + 2 of 7.  We don’t then need to check for 1 to 6.  n = 401 conforms to all three patterns: the 401st heptagonal number is (interestingly) 401401, the 406th 411481, their difference 10080 = 4×5×7×8×9.  I’m still wondering what use heptagonal numbers are!

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