Enigmatic Code

Programming Enigma Puzzles

Enigma 1524: Prime furniture

From New Scientist #2686, 13th December 2008 [link]

Some three-digit primes are formed from a one-digit prime followed by a two-digit prime, others from a two-digit prime followed by a one-digit prime, and some can be in both classes.

I recently bought three pieces of furniture, and each cost a different sum which was one of the last-mentioned type of prime (in euros), but the total I paid was not a prime.

In ascending order, what were the prices of my pieces of furniture?



4 responses to “Enigma 1524: Prime furniture

  1. Jim Randell 25 July 2012 at 7:33 am

    Here’s my original Perl solution. It’s one of the rare cases where I use Math::Combinatorics, rather than separate for loops. It runs in 34ms.

    use strict;
    use Enigma qw/prime/;
    use Math::Combinatorics;
    my @PRIMES = ();
    my ($A, $B, $C);
    for (200..799) {
      next unless prime($_);
      ($A, $B, $C) = split '', $_;
      next unless prime($A) and prime($C);
      next unless prime("$A$B") and prime("$B$C");
      push @PRIMES, $_;
    my ($primes, @set, $sum);
    $primes = Math::Combinatorics->new(count => 3, data => [@PRIMES]);
    while (@set = $primes->next_combination) {
      $sum = 0; map { $sum += $_ } @set;
      next if prime($sum);
      print join(' + ', sort { $a <=> $b } @set), " = $sum\n";

    Solution: The prices were €313, €317 and €373.

    • Jim Randell 25 July 2012 at 7:34 am

      Here’s a Python solution. It uses a prime sieve to select the initial primes. It runs in 43ms.

      from itertools import combinations
      from enigma import Primes, is_prime, printf
      # we're interested in 1, 2 and 3 digit primes
      primes = Primes(999)
      # find 3-digit primes, that can be written as 1+2 and 2+1 digit primes
      p3s = set()
      for p in primes:
        if p < 100: continue
        (a, b) = divmod(p, 10)
        if not(a in primes and b in primes): continue
        (a, b) = divmod(p, 100)
        if not(a in primes and b in primes): continue
      # now consider triples that sum to a non-prime
      for t in combinations(p3s, 3):
        s = sum(t)
        if is_prime(s): continue
        printf("sum{x} = {s}", x=sorted(t))
      • Hugh Casement 25 January 2016 at 10:41 am

        Am I missing something here?  If the last digits of the three primes are respectively 1, 1, 3, or 3, 3, 9, or 7, 7, 1, or 9, 9, 7, then the total ends in 5 so is not a prime.
        I found fourteen primes that fall into the required class, so there are numerous possibilities.
        Don’t we need a further condition or restriction?

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: