Enigmatic Code

Programming Enigma Puzzles

Enigma 1545: Dressed up to the nines

From New Scientist #2708, 16th May 2009 [link]

Hepta wrote down a three-digit number, Otto wrote down a larger number and Nonno wrote down a larger number still.

Hepta’s equalled 7 times a prime, Otto’s equalled 8 times another prime, and Nonno’s equalled 9 times another prime.

Between them, the three numbers used each of the digits 1-9 exactly once.

Furthermore, multiplying the three numbers together gave an answer which also used each of the digits 1-9 exactly once.

What were their numbers?

[enigma1545]

Advertisements

2 responses to “Enigma 1545: Dressed up to the nines

  1. Jim Randell 8 April 2012 at 8:58 pm

    Here’s my original Perl solution. It runs in 16ms.

    use Enigma qw/prime/;
    
    my ($H, $O, $N);
    my ($H7, $O8, $N9);
    my ($P);
    
    for ($H = 15;; $H++) {
      $H7 = $H * 7;
      last if length($H7) > 3;
      next if $H7 =~ /0/;
      next if $H7 =~ /(\d).*\1/;
      next unless prime($H);
    
      for ($O = int(($H7 + 1) / 8);; $O++) {
        next if $O == $H;
        $O8 = $O * 8;
        last if length($O8) != 3;
        next if $O8 =~ /0/;
        next if "$H7$O8" =~ /(\d).*\1/;
        next unless prime($O);
    
        for ($N = int(($O8 + 1) / 9);; $N++) {
          next if $N == $H or $N == $O;
          $N9 = $N * 9;
          last if length($N9) > 3;
          next if $N9 =~ /0/;
          next if "$H7$O8$N9" =~ /(\d).*\1/;
          next unless prime($N);
    
          $P = $H7 * $O8 * $N9;
          next if $P =~ /0/;
          next if $P =~ /(\d).*\1/;
    
          print "H7 = $H7 [7x$H], O8 = $O8 [8x$O], N9 = $N9 [9x$N], H7xO8xN9 = $P\n";
        }
      }
    }
    

    Solution: Hepta wrote down 413, Otto wrote down 568 and Nonno wrote down 927.

  2. Jim Randell 8 April 2012 at 9:04 pm

    And here’s a solution using Python that takes a slightly different approach. It runs in 37ms.

    from enigma import Primes, printf
    
    # look for 3 digit numbers that are the product of a prime and 7, 8, 9
    
    primes = Primes(1 + (1000 // 7))
    
    def products(n):
      d = {}
      for p in primes:
        q = p * n
        if not(q < 1000): break
        if not(99 < q): continue
        s = set(str(q))
        if len(s) != 3 or '0' in s: continue
        d[q] = p
      return d
    
    (h7, o8, n9) = (products(7), products(8), products(9))
    
    # choose h, o, n, so that they are increasing and pandigital
    for h in h7:
      d1 = set(str(h))
      for o in o8:
        if not(h < o): continue
        d2 = d1.union(str(o))
        if len(d2) != 6: continue
        for n in n9:
          if not(o < n): continue
          d3 = d2.union(str(n))
          if len(d3) != 9: continue
    
          # check the primes are different
          if len(set((h7[h], o8[o], n9[n]))) != 3: continue
    
          # check the product is pandigital
          p = h * o * n
          s = set(list(str(p)))
          if len(s) != 9 or '0' in s: continue
    
          printf("H={h} [7x{h7}] O={o} [8x{o8}] N={n} [9x{n9}] p={p}", h7=h7[h], o8=o8[o], n9=n9[n])
    

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: