Enigmatic Code

Programming Enigma Puzzles

Enigma 984: Answers on the back

From New Scientist #2139, 20th June 1998 [link]

I have 8 cards and each card has a whole number on the back. Not all the 8 numbers are the same. On the front of each card there is a true statement about the number on the back of the car. The eight statements are as follows:

“The number on the reverse of this card is…
… 2 plus the number of cards with an even number on them;
… the total of the 8 numbers, divided by 5;
… the number of cards with a prime on them;
… the number of cards with a number larger than 3 on them;
… the difference between the largest number on the cards and the second largest number on the cards;
… the difference between the smallest number on the cards and the second smallest number on the cards;
… the number of cards that have the largest number on them;
… the difference between the number of cards that have the smallest number on them and the number of cards that have the second smallest number on them.”

When a statement refers to a number on a card it means the number on the back of the card. And an extra clue, which you do not actually need, is that if card A has a higher number on it than card B then card A comes before card B in the above list of statements.

What are the 8 numbers on the cards? Give them in descending order.

[enigma984]

One response to “Enigma 984: Answers on the back

  1. Jim Randell 15 November 2019 at 7:48 am

    By “whole number” I assumed the setter meant “a positive integer”.

    The value on the first card has to be at least 2. If none of the other cards had even numbers then the first card would have to be 2, but then there would be 1 card with an even number, so it would have to be 3. Hence minimum value on the first card is 3. If all the cards had even numbers on, then the first card would be 10, so this gives us a limited range for the numbers on the cards.

    The following Python program runs in 205ms.

    Run: [ @repl.it ]

    from enigma import irange, subsets, is_prime, printf
    
    # consider the largest number (must be at least 2)
    for M in irange(3, 10):
      # choose the remaining cards
      for cards in subsets(irange(M, 1, step=-1), size=7, select="R"):
        cards = (M,) + cards
    
        # find the different numbers on the cards
        s = sorted(set(cards))
        # cards are not all the same
        if not(len(s) > 1): continue
    
        # check each of the statements:
    
        # 1. "2 plus the number of even cards"
        if not(cards[0] == 2 + sum(n % 2 == 0 for n in cards)): continue
    
        # 2. "the sum divided by 5"
        if not(cards[1] * 5 == sum(cards)): continue
    
        # 3. "the number of cards with a prime on them"
        if not(cards[2] == sum(is_prime(n) for n in cards)): continue
    
        # 4. "the number of cards with a number larger than 3 on them"
        if not(cards[3] == sum(n > 3 for n in cards)): continue
    
        # 5. "the difference between the largest number and second largest
        # number"
        if not(cards[4] == s[-1] - s[-2]): continue
    
        # 6. "the difference between the smallest number and the second
        # smallest number"
        if not(cards[5] == s[1] - s[0]): continue
    
        # 7. "the number of cards with the largest number on them"
        if not(cards[6] == cards.count(s[-1])): continue
    
        # 8. "the difference between the number of cards with the smallest
        # number on them, and the number of cards with the second smallest
        # number on them"
        if not(cards[7] == abs(cards.count(s[0]) - cards.count(s[1]))): continue
    
        printf("{cards}")
    

    Solution: The numbers on the cards are: 6, 4, 3, 2, 2, 1, 1, 1.

    There are no further solutions if 0 is allowed as a number on the cards. It is potentially possible that the last card could be 0, if there were the same number of cards with the smallest number and next smallest number.

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: