Enigmatic Code

Programming Enigma Puzzles

Enigma 1364: Four all

From New Scientist #2523, 29th October 2005

I have in mind four numbers, each of four different digits one of which is 4. For each of them, four of the statements below are true and four are false.

(1) The number is a fourth power.
(2) The number is divisible by four.
(3) The number consists of a two-digit square followed by a smaller two-digit square.
(4) The product of the four digits exceeds the fourth power of 2.
(5) The number does not have two or more different prime factors.
(6) Each of the four digits is a perfect square.
(7) The digits form an arithmetic progression.
(8) The sum of the digits is prime, or else the sum of the two digits of that sum is 4.

What are my four numbers?

This is a similar type of problem to Enigma 1775.

[enigma1364]

One response to “Enigma 1364: Four all

  1. Jim Randell 15 December 2013 at 9:37 am

    This Python program runs in 105ms.

    from enigma import irange, split, multiply, factor, Primes, printf
    
    # 4th powers (as ints)
    p4s = list(n ** 4 for n in irange(6, 9))
    
    # 2 digit squares (as strings)
    sqs = list(str(n ** 2) for n in irange(4, 9))
    
    # primes (max digit sum is 4 + 9 + 8 + 7 == 28)
    primes = Primes(28)
    
    # check 4 digit numbers
    for n in irange(1000, 9999):
      s = str(n)
    
      # one of the digits should be 4
      if '4' not in s: continue
    
      # calculate the digits (as integers)
      ds = split(s, int)
    
      # digits should be distinct
      if len(set(ds)) != 4: continue
    
      # compute prime factors
      fs = factor(n)
    
      # the sum of the digits
      sd = sum(ds)
      
      # check the statements
      ss = (
    
        # 1. the number is a 4th power
        n in p4s,
    
        # 2. the number is divisible by 4
        n % 4 == 0,
    
        # 3. the number consists of a 2-digit square followed by a smaller 2-digit square
        s[:2] in sqs and s[2:] in sqs and s[2:] < s[:2],
    
        # 4. the product of the 4 digits exceeds 2^4 == 16
        # (actually, it would be sufficient to check: 0 not in ds)
        multiply(ds) > 16,
    
        # 5. the number does not have two or more different prime factors
        len(set(fs)) < 2,
    
        # 6. each of the 4 digits is a perfect square
        # (they are all different, so must be: 0, 1, 4, 9)
        (0 in ds and 1 in ds and 9 in ds),
    
        # 7. the digits form an arithmetic progression
        len(set(ds[i + 1] - ds[i] for i in (0, 1, 2))) == 1,
    
        # 8. the sum of digits is prime, or else the sum of the two digits of that sum is 4
        (sd in primes or (sd > 9 and sum(divmod(sd, 10)) == 4)),
      )
    
      if ss.count(True) == 4:
        printf("n={n} {ss} {fs}")
    

    Solution: The four numbers are 4096, 4567, 4936 and 8164.

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: