# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1523: Dicey numbers

From New Scientist #2685, 6th December 2008 [link]

I threw a small number of standard dice and noted that the grand total of spots on the tops was a perfect square. Then I put the dice in a row in increasing order of their tops and wrote down the large number so formed (for example, 12224…).

Then I repeated the exercise with double the number of dice. Again the total number of spots was a perfect square, and again I wrote down the number formed by the tops in increasing order. It turned out that the second number I wrote down was the square of the first.

Incidentally (although you do not need to know this) throughout the exercise I threw at most one 6.

What was the second number that I wrote down?

[enigma1523]

### One response to “Enigma 1523: Dicey numbers”

1. Jim Randell 30 July 2012 at 7:51 am

The following Python code runs in 100ms.

It checks for solutions up to a specified “small number” of dice. The default is 10, but you can specify a different number on the command line. If you want to find only the first solution you can add a call to [[ `sys.exit()` ]] at the end of the [[ `check()` ]] function.

```import re
import sys
from enigma import irange, is_square, printf

# check to see if this sequence is a solution
# s - digit sequence as a string
def check(s):
# the sum of the digits must be a square
t = sum(int(i) for i in s)
if not is_square(t): return
# square the number
s2 = str(int(s) ** 2)
if len(s2) != 2 * len(s): return
# the number must be formed from increasing dice (with at most one 6)
if not re.match(r'^1*2*3*4*5*\$' if '6' in s else r'^1*2*3*4*5*6?\$', s2): return
# again, the sum of the digits should be a square
t2 = sum(int(i) for i in s2)
if not is_square(t2): return
printf("[{n}] {s} => {t}, {s2} => {t2}", n=len(s))
# sys.exit(0) # if you only want the first answer

# construct n-digit sequences of increasing dice digits (with just one 6)
# n - number of digts we're looking for
# s - current string of digits
# d - final digit (as an integer)
def solve(n, s='', d=1):
if len(s) == n:
check(s)
return
if d == 6:
return
for i in irange(d, 6):
solve(n, s + str(i), i)

MAX = 10 if len(sys.argv) < 2 else eval(sys.argv[1])

for n in irange(1, MAX):
solve(n)
```

Solution: The second number is 1111111155555556.

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