Enigmatic Code

Programming Enigma Puzzles

Enigma 1606: Nightmare!

From New Scientist #2771, 31st July 2010 [link]

You are in a locked room, seated at a table on which stands a two-pan balance. One of the pans holds a number of steel ball bearings, each weighing the same whole number of grams greater than 1.

If you could determine the total weight of the balls, you could work out the number of balls there are. This number is particularly important to you as it is the same number as the combination security lock on the door.

Next to the balance is a set of weights, each weighing the same whole number of grams, and together totalling 4 kilograms. If you were allowed to use the balance, these would enable you to work out the weight of the balls. But if you disturb the balance, in any way, it will explode catastrophically.

Thankfully, with those particular weights you already have all the information you need to work out the number of balls with absolute certainty.

What number opens the security lock and grants your freedom?

[enigma1606]

Advertisements

5 responses to “Enigma 1606: Nightmare!

  1. jimrandell 5 January 2012 at 11:36 am

    The following Python code finds the solution in 36ms.

    # if you know the total weight of the balls you can determine how many
    # there are so there are p balls, each weighing p grams, for some
    # prime p.
    
    from collections import defaultdict
    from enigma import Primes, irange, divisors, factor, printf
    
    # look for numbers less than 4000 that are the squares of primes
    ps = list(p * p for p in Primes(63))
    
    # for the weights consider divisors of 4000
    for n in divisors(4000):
      # now see if there is exactly one p that falls into its own category
      d = defaultdict(list)
      for p in ps:
        (c, r) = divmod(p, n)
        d[c if r > 0 else -c].append(p) # allow for exact balances
      l = list(x for x in d.values() if len(x) == 1)
      if len(l) != 1: continue
      p = l[0][0]
      printf("there are {m}x {n}g weights", m=4000 // n)
      printf("{p}g falls into a {n}g category by itself")
      printf("{p}g = {f} [balls, g]", f=factor(p))
    

    Solution: The number is 53.

  2. Hugh Casement 23 March 2016 at 3:16 pm

    Jim, the logic of this is lost on me.  If we can assume the total weight of the balls is less than 4000 grams, then p could be any prime up to 61.  But the wording implies we could weigh perhaps only five of them if they’re 5-gram weights, in which case the total weight could be greater.  I’d be tempted to measure the diameter of a ball and assume a density 7.85 g/cm³.

    • Hugh Casement 27 March 2016 at 10:48 am

      Could somebody please explain this one to me?  As far as I can see, p can be any prime: what’s so special about 53?

    • Jim Randell 27 March 2016 at 11:46 am

      If we knew the total weight of the balls we would know how many there are. As they each weigh a whole number of grams the total weight must be the square of a prime, so that there are the same number of balls as each ball weighs in grams. (I’m glossing over the fact we could probably distinguish between 7 balls each weighing 571 grams from 571 balls each weighing 7 grams by inspection – but perhaps the balls are hidden from view in a sealed opaque container of negligible weight. I think the wording of the puzzle could be clearer in this respect).

      So, the possibilities for the total weight of the balls (in grams) is: 4, 9, 25, 49, 121, 169, 289, 361, 529, 841, 961, 1369, 1681, 1849, 2209, 2809, 3481, 3721.

      Next we have a set of identical weights totalling 4000g, which (if we were allowed to use them) would allow us to determine the total weight of the balls.

      If there were 2 weights of 2000g each we could determine if the total weight of the balls was more of less than 2000g, but there are 14 possible total weights below 2000g and 4 above, so that wouldn’t let us determine the total weight of the balls.

      If there were 4 weights of 1000g each we get:

      < 1000g: 4, 9, 25, 49, 121, 169, 289, 361, 529, 841, 961 (11 possibilities)
      < 2000g: 1369, 1681, 1849 (3 possibilities)
      < 3000g: 2209, 2809 (2 possibilities)
      < 4000g: 3481, 3721 (2 possibilities)

      If there were 5 weights of 800g each we get:

      < 800g: 4, 9, 25, 49, 121, 169, 289, 361, 529 (9 possibilities)
      < 1600g: 841, 961, 1369 (3 possibilities)
      < 2400g: 1681, 1849, 2209 (3 possibilities)
      < 3200g: 2809 (1 possibility)
      < 4000g: 3481, 3721 (2 possibilities)

      So as we were adding 800g weights to the balance, the balance would change when we added the 4th weight to the pan, telling us the total weight of the balls was between 2400g and 3200g. The only possible weight in this interval is 2809g = 53 balls × 53g.

      We are told that the particular set of weights will allow us to work out the total weight of the balls without needing to do the weighing. So if we had this particular set of weights we would know that the total weight of the balls must be 2809g, as it is the only possible total weight that falls into its own interval.

      If there were 8 weights of 500g we get:

      < 500g: 4, 9, 25, 49, 121, 169, 289, 361 (8 possibilities)
      < 1000g: 529, 841, 961 (3 possibilities)
      < 1500g: 1369 (1 possibility)
      < 2000g: 1680, 1849 (2 possibilities)
      < 2500g: 2209 (1 possibility)
      < 3000g: 2809 (1 possibility)
      < 3500g: 3481 (1 possibility)
      < 4000g: 3721 (1 possibility)

      In this case, if we were allowed to add 500g weights to the balance, then, if the balance tipped when we added the third, fifth, sixth, seventh or eighth weights we would know the total weight of the balls (as in each case there is only one possibility). However, without doing the weighing, we can’t determine the total weight of the balls, as there are multiple intervals with only a single possibility, so this is not the set of weights we are presented with.

      Similarly, with all sets of lighter weights there are multiple intervals with only a singular possibility, so the only possible set of weights is 8× 500g, and the corresponding total weight of the balls is 2809g, which means there are 53 balls, each weighing 53g.

      • Hugh Casement 27 March 2016 at 12:36 pm

        .Thanks for spelling it out, Jim.  As you say, the wording could have been clearer.  I imagined being able to pick out a certain number of balls to weigh — in theory, that is, as we’re not allowed to.
        53 g is a large steel ball: I calculate a diameter of 59/64 inch.  But of course they would easily fit in a cubical box 10 cm on a side

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: