Enigmatic Code

Programming Enigma Puzzles

Enigma 526: Ewe to move

From New Scientist #1678, 19th August 1989 [link] [link]

Each of the four fields at Sunny Meadows Farm contains some sheep and some cows. On the gate of each field is hung a sign saying what fraction of the animals in that field are sheep. The signs are 1/2, 1/3, 2/3, 1/4.

Farmer Gillian explained that if she exchanged the signs on [any] two of the fields then, by simply moving some sheep from one of the two fields to the other, she could return to a situation where each sign again correctly indicated the fraction of the animals that were sheep in that field.

As I walked round I noticed that the total number of animals on the farm was between 300 and 350.

How many sheep, and how many cows, were on the farm?

I added the “any” in square brackets, as without it there are many solutions to the puzzle.

[enigma526]

2 responses to “Enigma 526: Ewe to move

  1. Jim Randell 18 November 2019 at 8:38 am

    As originally published the puzzle has many solutions. The inclusion of the word “any” makes this a viable puzzle and is probably what the setter had in mind.

    This recursive Python program runs in 103ms.

    Run: [ @repl.it ]

    from enigma import irange, div, printf
    
    # how many whole sheep to transfer to a field with a of n sheep
    # to make b of m sheep, and swap ratios in the fields
    def sheep(a, n, b, m):
      x = div(b * n - a * m, m - b)
      if x is None: return
      if n * (b - x) == a * (m - x): return x
    
    # label the fields
    fields = [ (1, 4), (2, 3), (1, 3), (1, 2) ]
    
    # solve for the remaining field fractions
    # fs = fields to process
    # m = maximum number of animals in all fields
    # ss = collect (sheep, total) for each field
    def solve(fs, m, M, ss=[]):
      if not fs:
        if not(sum(t for (s, t) in ss) < m):
          yield ss
      else:
        (a, b) = fs[0]
        # choose a total number of animals in this field
        # it must be divisible by b
        for t in irange(b, M, step=b):
          # so the number of sheep is...
          s = div(a * t, b)
          # check we can swap ratios by swapping sheep
          if all(sheep(s, t, x, y) for (x, y) in ss):
            yield from solve(fs[1:], m, M - t, ss + [(s, t)])
    
    # solve for the given parameters
    for ss in solve(fields, 300, 350):
      # calculate the total number of animals and the number of sheep and cows
      T = sum(t for (s, t) in ss)
      Ts = sum(s for (s, t) in ss)
      Tc = T - Ts
      # output solution
      printf("sheep = {Ts}, cows = {Tc} [{ss}]")
    

    Solution: There are 161 sheep, and 168 cows on the farm.

    So the total number of animals is 329.

    The distribution of animals in the fields is:

    A: 42 sheep, 42 cows; 84 total
    B: 21 sheep, 42 cows; 63 total
    C: 84 sheep, 42 cows; 126 total
    D: 14 sheep, 42 cows; 56 total

  2. GeoffR 19 November 2019 at 5:46 pm
    % A Solution in MiniZinc
    include "globals.mzn";  
    
    % Field No. Start Ratio  No. sheep No. cows total animals
    %   F1       (1/2)          s1        c        t1
    %   F2       (1/3)          s2        c        t2
    %   F3       (2/3)          s3        c        t3
    %   F4       (1/4)          s4        c        t4
    
    % Numbers of sheep and cows in each field
    var 1..100:s1; var 1..100:s2; var 1..100:s3; var 1..100:s4;  
    var 1..100:c; 
    
    % Field sub-total numbers
    var 1..200: t1; var 1..200: t2; var 1..200: t3; var 1..200: t4;
    
    % Six variable numbers of sheep to move i.e. d1 to d6
    % For six moves: F1 to F2, F1-F3, F1-F4, F2-F3, F2-F4, F3-F4
    var 1..100: d1; var 1..100: d2; var 1..100: d3; 
    var 1..100: d4; var 1..100: d5; var 1..100: d6; 
    
    % Total number of animals
    var 1..250: tot_sheep;
    var 1..250: tot_cows;
    
    var 300..350: tot_animals;
    
    % Starting total for sheep and cows in each field
    constraint s1 + c == t1;    
    constraint s2 + c == t2; 
    constraint s3 + c == t3; 
    constraint s4 + c == t4;
    
    % set initial ratios for sheep/total animals in each field
    constraint 2 * s1 == t1 /\ 3 * s2 == t2 
    /\ 3 * s3 == 2 * t3 /\ 4 * s4 == t4;
    
    % Move d1 sheep from field F1 to field F2 & change ratios 1/2 and 1/3
    constraint 3 * (s1 - d1) == (t1 - d1) /\ 2 * (s2 + d1) == (t2 + d1)
    
    % % Or Move d2 sheep from field F1 to field F3 & change ratios 1/2 and 2/3
     \/ 3 * (s1 - d2) == 2 * (t1 - d2) /\ (t3 + d2) == 2 * (s3 + d2)
    
    % % Or Move d3 sheep from F1 to F4 & change ratios 1/2 and 1/4
     \/ 4 * (s1 - d3) == (t1 - d3) /\  2 * (s4 + d3) == (t4 + d3)
    
    % % Or Move d4 sheep from F2 to F3 & change ratios 1/3 and 2/3
     \/ 3 * (s2 + d4) == 2 * (t2 + d4)/\ 3 *(s3 - d4) == (t3 - d4)
    
    % % Or Move d5 sheep from F2 to F4 & change ratios 1/3 and 1/4
     \/ 4 * (s2 - d5) == (t2 - d5) /\ 2 * (s4 + d5) == (t4 + d5)
    
    % % Or Move d6 sheep from F3 to F4 & change ratios 2/3 and 1/4
     \/ 4 * (s3 - d6) == (t3 - d6) /\ 2 * (s4 + d6) == (t4 + d6);
    
    constraint tot_sheep = s1 + s2 + s3 + s4;
    constraint tot_cows = 4 * c;
    constraint tot_animals = tot_sheep + tot_cows;
    
    solve satisfy;
    
    output [ 
    "Field 1 = " ++ show(s1) ++ " sheep, " ++ show(c) ++ " cows, "
    ++ show (s1 + c) ++ " total" ++ "\n" ++ 
    
    "Field 2 = " ++ show(s2) ++ " sheep, " ++ show(c) ++ " cows, "
    ++ show (s2 + c) ++ " total" ++ "\n"
    
    ++ "Field 3 = " ++ show(s3) ++ " sheep, " ++ show(c) ++ " cows, "
    ++ show (s3 + c) ++ " total" ++ "\n" ++
    
    "Field 4 = " ++ show(s4) ++ " sheep, " ++ show(c) ++ " cows, "
    ++ show (s4 + c) ++ " total" ++ "\n"
     
    ++ "\n" ++ "Total sheep = " ++ show(tot_sheep) 
    ++ "\n" ++ "Total cows = " ++ show(tot_cows) 
    ++ "\n" ++ "Total animals = " ++ show(tot_animals) 
     ];
     
    % Field 1 = 42 sheep, 42 cows, 84 total
    % Field 2 = 21 sheep, 42 cows, 63 total
    % Field 3 = 84 sheep, 42 cows, 126 total
    % Field 4 = 14 sheep, 42 cows, 56 total
    
    % Total sheep = 161
    % Total cows = 168
    % Total animals = 329
    % ----------
    % ==========
    % Finished in 627msec
    
    
    
    

    I found this solution would not run under the Geocode solver, but did run OK under the Chuffed solver.

    Keeping the number of cows the same in the four fields, I got the same single solution as Jim

    I noticed that the digit 7 seems to be the key to this enigma, since:
    1) All the totals of (sheep, cows,total) are divisible by 7 for each of the four fields
    2) The possible transfers of sheep between fields are divisible by 7
    3) The total sheep(161), cows(168) and total animals(329) are all are divisible by 7

    I also found that by varying the number of cows in the four fields, this gave multiple solutions,
    so the number of cows in each field must be the same to give a single solution.

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: