Enigmatic Code

Programming Enigma Puzzles

Enigma 1408: Consecutive digits

From New Scientist #2568, 9th September 2006

I invite you to find:

(a) a five-digit integer (not starting with zero) that consists of five consecutive digits, which are not necessarily in numerical order; when this integer is multiplied by 8 the result should be a five-digit integer that consists of the other five digits;

(b) a five-digit integer that consists of five consecutive digits, which are not necessarily in numerical order; when this integer is divided by 8 the result should be a five-digit integer (not starting with zero) that consists of the other five digits.

Which five-digit integers have you found? In each case the answer required is the integer that consists of five consecutive digits.

[enigma1408]

Advertisements

3 responses to “Enigma 1408: Consecutive digits

  1. Jim Randell 29 July 2013 at 10:35 am

    This Python program runs in 37ms.

    from itertools import permutations
    from enigma import irange, nconcat, printf
    
    # choose the starting digit
    for i in irange(0, 5):
      # permute the 5 consecutive digits
      for ds in permutations(irange(i, i + 4)):
        if ds[0] == 0: continue
        # make an integer
        n = nconcat(ds)
    
        # (a) n and 8n should be all 10 digits
        s = str(n) + str(8 * n)
        if len(s) == 10 and len(set(s)) == 10:
          printf("(a) n={n} n*8={n8}", n8=s[5:])
    
        # (b) n and n/8 should be all 10 digits
        (n8, r) = divmod(n, 8)
        if r == 0:
          s = str(n) + str(n8)
          if len(s) == 10 and len(set(s)) == 10:
            printf("(b) n={n} n/8={n8}")
    

    Solution: The numbers are (a) 12345; (b) 87456.

  2. Naim Uygun 29 July 2013 at 2:24 pm
    #For part (a)
    from itertools import permutations
    p=set("9876543210")
    for a,b,c,d,e  in permutations(p,5):
      if a =='0': continue
      abcde=a+b+c+d+e
      s1=sorted(abcde)
      m=set(abcde)
      if int(s1[4])-int(s1[3]) != 1:continue
      if int(s1[3])-int(s1[2]) != 1: continue
      if int(s1[2])-int(s1[1]) != 1: continue
      if int(s1[1])-int(s1[0]) != 1: continue
      x=int(abcde)*8
      s2=str(x)
      n=set(s2)
      if len(s2) != 5 :  continue  
      if len(set(s1)  & n) != 0 : continue
      print(abcde," x  8 = ",x)
    
  3. Naim Uygun 29 July 2013 at 3:15 pm
    ...
    # For part (b)
    from itertools import permutations
    p=set("9876543210")
    for a,b,c,d,e  in permutations(p,5):
      if a=='0': continue
      abcde=a+b+c+d+e
      s1=sorted(abcde)
      m=set(s1)
      if int(s1[4])-int(s1[3]) != 1:continue
      if int(s1[3])-int(s1[2]) != 1: continue
      if int(s1[2])-int(s1[1]) != 1: continue
      if int(s1[1])-int(s1[0]) != 1: continue
      y=int(abcde)/8
      if int(y) != y: continue
      s2=str(y)
      #Remove .0
      ns=s2[:len(s2)-2]
      n=set(ns)
      if len(n) != 5 : continue
      if len(m & n) != 0 : continue
      print(abcde," / 8 = ",ns)
    

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: