# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1564: My favourite number

From New Scientist #2727, 26th September 2009 [link]

My favourite number consists of several digits and is not included on my list of five whole numbers (comprising non-zero digits) written below in capital letters, where different letters stand for different digits and the same letter stands for the same digit.

I can tell you that although my favourite number is smaller than the BEST number, which is twice as GOOD, and larger than the ODD prime number, there is a BETTER number that is bigger than the OTHER number, which is exactly 25 times my favourite number.

What is my favourite number?

[enigma1564]

### 2 responses to “Enigma 1564: My favourite number”

1. jimrandell 22 February 2012 at 8:23 am

The following Python program tests all the specified conditions (although some of them don’t reduce the search space). It runs in 36ms.

```# the following numbers comprise non-zero digits

# ODD -> prime
# BEST -> favourite < BEST
# GOOD -> GOOD * 2 = BEST
# BETTER -> BETTER > OTHER
# OTHER -> OTHER = 25 * favourite

from itertools import permutations
from enigma import concat, split, is_prime, printf

R = 5 # as OTHER = 25 * favourite, and R cannot be 0

d1 = set(range(1, 10)).difference((R,))
# ODD is prime
for (O, D) in permutations(d1, 2):
ODD = int(concat(O, D, D))
if not is_prime(ODD): continue
d2 = d1.difference((O, D))
# GOOD * 2 = BEST (so G < 5)
for G in d2:
if not(G < 5): continue
GOOD = int(concat(G, O, O, D))
BEST = GOOD * 2
(B, E, S, T) = split(BEST, int)
d3 = d2.difference((G, B, E, S, T))
if len(d3) != 1: continue
BETTER = int(concat(B, E, T, T, E, R))
H = d3.pop()
OTHER = int(concat(O, T, H, E, R))
if not(BETTER > OTHER): continue
(favourite, r) = divmod(OTHER, 25)
if r > 0: continue
if not(favourite < BEST): continue

printf("{favourite} [ODD={ODD} GOOD={GOOD} BEST={BEST} BETTER={BETTER} OTHER={OTHER}]")
```

Solution: Your favourite number is 3295.

2. geoffrounce 21 March 2017 at 3:54 pm

Here is a MiniZinc solution:

``` % A solution in MiniZinc

include "globals.mzn";

predicate is_prime(var int: x) = x > 1 /\
forall(i in 2..1 + ceil(sqrt(int2float(ub(x))))) (
(i < x) -> (x mod i > 0));

var 1..9: B;  var 1..9: E; var 1..9: S; var 1..9: T; var 1..9: G;
var 1..9: O;  var 1..9: D; var 1..9: R; var 1..9: H;

% favourite number - max value must be less than 99999/25 ie < 4000
% and favourite number is greater than 400, since OTHER is greater than 10000
var 400..4000 : favnum;

array[1..9] of var int : fd = [B,E,S,T,G,O,D,R,H];

constraint all_different(fd);

constraint is_prime(O*100 + D*10 + D);

% GOOD * 2 = BEST
constraint (G*1000 + 110*O + D) * 2 == (1000*B + 100*E + 10*S + T);

%  BETTER > OTHER
constraint (B*100000 + E*10000 + T*1000 + T*100 + E*10 + R) >
(O*10000 + T*1000 + H*100 + E*10 + R);

% favnum < BEST and OTHER = 25 * favnum
constraint  favnum < (B*1000 + E*100 + S*10 + T) /\
(O*10000 + T*1000 + H*100 + E*10 + R) ==
25 * favnum;

solve satisfy;

output["MY favourite number = " ++ show(favnum) ++ "\n"
++ "BEST = " ++ show(B),show(E),show(S),show(T) ++ "\n"
++ "GOOD = " ++ show(G), show(O),show(O), show(D) ++ "\n"
++ "OTHER = " ++ show(O),show(T),show(H),show(E),show(R)  ++ "\n"
++ "BETTER = " ++ show(B),show(E),show(T),show(T),show(E),show(R)
++ "\n" ++ "ODD = " ++ show(O),show(D),show(D)];

% MY favourite number = 3295
% BEST = 9762
% GOOD = 4881
% OTHER = 82375
% BETTER = 972275
% ODD = 811
% ----------
% Finished in 99msec
```