Enigmatic Code

Programming Enigma Puzzles

Tantalizer 335: Pocket money

From New Scientist #886, 21st February 1974 [link]

Teddy and his brothers have set their hearts on a paint-box, a fire-engine, and a space-gun. So, when they received 20p each for digging the garden recently, each earmarked a different whole number of pennies for each toy. (I mean that each divided his 20p differently, not that no two boys allocated the same number [of pennies] to one toy).

Each, alas, fell far short of the sum needed. But then they had the happy idea of pooling resources. Each had devoted his largest sum to the space-gun which came to 52p in total, and, hooray, the gun cost 52p … Each had earmarked his smallest sum for the paint-box and that total too exactly bought it. Similarly they also had exactly enough for the fire-engine.

If I just add that Teddy’s 3p was the smallest contribution to anything, can you work out the price of the fire-engine?


One response to “Tantalizer 335: Pocket money

  1. Jim Randell 20 October 2021 at 9:06 am

    Each brother divides (the entirety of) their 20p into 3 increasing amounts, which are to go towards (in ascending order), the paint box, the fire engine, and the space gun. We can use the [[ decompose() ]] function from the enigma.py library to do this.

    We then select a decomposition containing 3p for Teddy, and some decompositions with all values greater than 3p for the brothers, and when we find a total contribution of 52p for the space gun we have found the solution.

    In the following Python 3.10 code I decided to use structural pattern matching (newly introduced in Python 3.10) to identify the solution condition. It is a powerful construct (and this use of it is only the tip of the iceberg), but it does mean the code will only run in Python 3.10 (or later), and may be confusing to people who have not encountered it before.

    Run: [ @replit ] (a Python <3.10 version)

    from enigma import decompose, filter2, unpack, subsets, unzip, printf
    # consider 3-way splits of 20p
    # collect those with first value of 3 (for Teddy)
    # and those with first value >3 (for the brothers)
    (ns2, ns3) = filter2(unpack(lambda x, y, z: x == 3), decompose(20, 3, sep=1, min_v=3))
    # choose the values for Teddy
    for t in ns2:
      # choose values for the brothers
      for bs in subsets(ns3, min_size=2, fn=list):
        # calculate the sums for each toy
        toys = list(sum(vs) for vs in unzip([t] + bs))
        # the space gun cost 52p
        match toys:
          case (_, _, 52):
            printf("toys = {toys} [teddy = {t}; brothers = {bs}]")

    Solution: The price of the fire engine is 28p.

    Teddy’s split is: (3, 4, 13)

    And the splits for his 4 brothers are: (4, 5, 11), (4, 6, 10), (4, 7, 9), (5, 6, 9).

    Giving the totals: paint box = 20p; fire engine = 28p; space gun = 52p.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: