Enigmatic Code

Programming Enigma Puzzles

Enigma 1419: Lying about my age

From New Scientist #2579, 25th November 2006

The word processor on which I produced this Enigma developed a fault yesterday and it still needs to be repaired. Whenever I type a positive whole number it actually reduces it by a fixed whole number percentage and then, to the nearest whole number, it prints that answer.

So, for example, when I typed my age yesterday it actually printed 49, which was 7 less than it should have been.

How old am I?

[enigma1419]

Advertisements

3 responses to “Enigma 1419: Lying about my age

  1. Jim Randell 22 June 2013 at 10:28 am

    This puzzle requires careful reading to make sure you know what’s going on – I missed a subtlety in my first attempt. I solved this at the time it was published using Perl, but here’s the same approach (but slightly neater) in Python. It runs in 36ms.

    from itertools import count
    from enigma import irange, printf
    
    # for a fraction f the behaviour is that any positive integer n in the
    # input is replaced by int(f * n + 0.5) in the output, let's call this
    # function r(f, n) (as it is a reduction of n).
    
    # so if Susan's age is x0, she enters x0 into the word processor and
    # it inserts x1 = r(f, x0) into the text, and d1 = x0 - x1
    # now when entering the puzzle itself Susan enters "when I typed my age
    # yesterday it actually printed x1 which was d1 less than it should have
    # been", the the output is "... x2 which was d2 less..." where:
    # x2 = r(f, x1) = 49, d2 = x1 - x2 = 7
    
    def r(f, n):
      return int(f * n + 0.5)
    
    for p in irange(1, 99):
      f = float(p) / 100
      for x0 in count(int(48.5 / f)):
        x1 = r(f, x0)
        d1 = x0 - x1
        x2 = r(f, x1)
        d2 = x1 - x2
        if d2 > 7 or x2 > 49: break
        if x2 == 49 and d2 == 7:
          printf("p={p}% x0={x0} x1={x1} d1={d1} x2={x2} d2={d2}")
    

    Solution: Your correct age is 64.

    There are two percentage values which give this result: 87% (a 13% reduction) and 88% (a 12% reduction).

  2. Brian Gladman 22 June 2013 at 3:18 pm

    I wondered what the subtlety was so I tried it – it a neat twist that I also didn’t see first time round. Here is my version

    # return the possible inputs when 'out' has been printed
    def input_for(out, p):
      # set the limits on the possible input values
      for i in range(int(100 * out / p), int(100 * (out + 1) / p + 1)):
        # yield any input values that print the specified value
        if round(i * p / 100) == out:
          yield i
    
    # the percentage reduction
    for p in range(99, 0, -1):
      # the possible difference values misprinted as 7
      for difference in input_for(7, p):
        # the possible age values misprinted as 49
        for misprinted_age in input_for(49, p):
          # but this misprinted age was itself misprinted yesterday
          for age in input_for(misprinted_age, p):
            # check the constraint
            if misprinted_age + difference == age:
              print('Age: {:d} ({:d}%).'.format(age, p))
    
  3. Hugh Casement 25 September 2014 at 8:53 am

    Much too subtle for me. Nowhere does the puzzle appear to state that a number is reduced twice.
    Evidently “whole number” means integer, but I can’t work out what function the word ‘actually’ is performing. Definitely flawed!

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: