Enigmatic Code

Programming Enigma Puzzles

Enigma 513: Less than a bargain

From New Scientist #1665, 20th May 1989 [link]

The fruit stall proclaimed, “Our fruit is so cheap it is even less than a bargain”, and so it had a good number of customers.

Hannah bought an apple and two bananas and yet spent less than Sarah who bought an orange and a 10-pence lemon. Joan bought 10 apples, 11 bananas and two oranges and yet did not spend all the 107 pence in her purse. Alan bought three apples, two bananas and an orange and his bill was less than 30 pence. Only Mot was unlucky: he tried to buy eight apples, seven bananas and two oranges, but they came to more than the 79 pence in his pocket.

Each piece of fruit cost a whole number of pence.

What was the cost of each apple, banana and orange?

[enigma513]

3 responses to “Enigma 513: Less than a bargain

  1. Jim Randell 19 August 2019 at 8:42 am

    We can assume all costs are positive. From the inequality for J we can see that apple ≤ 9 and banana ≤ 8. From the inequality for A we get orange ≤ 24. This gives us a solution space of 9×8×24 = 1728 values, which can easily be checked programatically.

    This Python program runs in 95ms.

    Run: [ @repl.it ]

    from itertools import product
    from enigma import irange, printf
    
    # choose prices for an apple, a banana, an orange
    for (apple, banana, orange) in product(irange(1, 9), irange(1, 8), irange(1, 24)):
    
      # H < S
      if not(apple + 2 * banana < orange + 10): continue
      # J < 107
      if not(10 * apple + 11 * banana + 2 * orange < 107): continue
      # A < 30
      if not(3 * apple + 2 * banana + orange < 30): continue
      # M > 79
      if not(8 * apple + 7 * banana + 2 * orange > 79): continue
    
      printf("apple = {apple}, banana = {banana}, orange = {orange}")
    

    Solution: An apple costs 5p. A banana costs 4p. An orange costs 6p.

  2. GeoffR 19 August 2019 at 9:23 am
    % A Solution in MiniZinc
    include "globals.mzn";
    
    var 1..25: apple;
    var 1..25: orange;
    var 1..25: banana;
    
    int: lemon = 10;
    
    constraint 1 * apple + 2 * banana < 1 * orange  + 1 * lemon;
    constraint 10 * apple + 11 * banana + 2 * orange < 107;
    constraint 3 * apple + 2 * banana + 1 * orange < 30;
    constraint 8 * apple + 7 * banana + 2 * orange > 79;
    
    solve satisfy;
    
    output ["One apple = " ++ show(apple) ++ " pence"
    ++ "\nOne orange = " ++ show(orange) ++ " pence"
    ++ "\nOne banana = " ++ show(banana) ++ " pence"];
    
    % One apple = 5 pence
    % One orange = 6 pence
    % One banana = 4 pence
    % ----------
    % ==========
    
  3. Paul Cleary 20 August 2019 at 2:02 pm

    A solution in Mma.

    AbsoluteTiming[Solve[{a+2b<o+10,10a+11b+2o<107,3a+2b+o<30,8a+7b+2o>79,a>0,b>0,o>0},{a,b,o},Integers]]
    
    {0.013597,{{a->5,b->4,o->6}}}
    

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: