Enigmatic Code

Programming Enigma Puzzles

Enigma 1317: Eight times

From New Scientist #2475, 27th November 2004

I invite you to find a 5-digit number consisting of five different digits (not starting with zero) which when multiplied by 8 produces another 5-digit number consisting of the other 5 digits; in addition the sum of the digits of your 5-digit number must be greater than the sum of the digits of the product.

Which 5-digit number have you found? (Remember that the answer required as the number as it was before the multiplication).

[enigma1317]

Advertisements

5 responses to “Enigma 1317: Eight times

  1. Jim Randell 13 June 2014 at 7:18 am

    This Python program does an exhaustive search and runs in 109ms. Although you can use a bit of analysis to significantly reduce the search space.

    from itertools import permutations
    from enigma import concat, split, printf
    
    digits = "0123456789"
    for s in permutations(digits, 5):
      if s[0] == '0': continue
      n = concat(*s)
      m = str(int(n) * 8)
      if len(m) != 5: continue
      if not(concat(*(sorted(n + m))) == digits): continue
      if not(sum(split(n, int)) > sum(split(m, int))): continue
      printf("{n} * 8 = {m}")
    

    Solution: The number is 10679.

    • Jim Randell 15 June 2014 at 10:43 pm

      Here’s my Python solution that uses a bit of analysis. It only needs to find permutations of the last three digits of the number. The number of function calls is reduced from 279,222 in the above program down to 1,510, and the internal runtime from 44.3ms to 0.8ms.

      from itertools import permutations
      from enigma import irange, nconcat, split, printf
      
      # b = 8 * a, both a and b are 5 digits, together they are pandigital
      # max(b) = 98765
      # max(a) = 12345
      # a1 = 1
      # a2 = 0 or 2
      # sum(digits) = 45, so sum(digits(a)) > 22
      
      digits = set(irange(0, 9))
      
      a1 = 1
      for a2 in (0, 2):
        for a345 in permutations(digits.difference((a1, a2)), 3):
          # sum of a's digits
          sa = a1 + a2 + sum(a345)
          if sa < 23: continue
          a = nconcat(a1, a2, *a345)
          b = a * 8
          if b > 98765: continue
          # sum of b's digits
          db = split(b, int)
          sb = sum(db)
          if not(sa > sb): continue
          # all digits should be represented
          if len(set((a1, a2) + a345 + tuple(db))) != 10: continue
      
          printf("{a} * 8 = {b}")
      
  2. Paul 13 June 2014 at 8:04 am

    MMA Code

    Do[a=IntegerDigits[8n];
    b=Total[i=IntegerDigits[n]];
    c=Sort[Complement[{1,2,3,4,5,6,7,8,9,0},a]];
    d=Total[a];
    If[b>d&&c==Sort[i],Print[n]],{n,10000,12499}]
    

    10679
    10ms

    Paul.

  3. Naim Uygun 13 June 2014 at 9:51 am
    #Output: 10679 x 8 = 85432
    from itertools import permutations
    
    for p in permutations("9876543210",5):
    
        if p[0]=='0': continue
        
        number=int("".join(p))
        product=number*8
    
        s=list(str(product))
        if len(s) != 5:continue
        
        s1=set(p)
        s2=set(s)
        
        if  len(s1 & s2) != 0 : continue
         
        n1=[int(p[i]) for i in range(0,5)]
        n2=[int(s[i]) for i in range(0,5)]
        
        if sum(n1) <= sum(n2): continue
        print(number,"x",8,"=",product)
        
    
  4. arthurvause 15 June 2014 at 11:46 am
    print [n for n in xrange(10234,100000/8)
           if sum(map(int, str(n))) > sum(map(int, str(8*n)))
           and set(str(n))|set(str(n*8))==set('0123456789')]
    

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: