Enigmatic Code

Programming Enigma Puzzles

Enigma 1742: Chip-chop

From New Scientist #2910, 30th March 2013 [link]

I have a seven-figure number that uses seven consecutive digits in some order. Starting at the left and deleting a digit leaves a six-digit number, then deleting the right-end digit leaves a five-digit number, then deleting the left-hand end digit leaves a four-digit number, and so on, alternating sides until a single digit is left. Looking at the list of seven numbers obtained in this way I see that they are all odd, and that only the six-digit number is divisible by 3 (but not 9). Surprisingly, if I had carried out the process starting by deleting the right-hand end digit and then the left, and so on down to a single digit, all the above facts would still be true.

What number did I start with?

[enigma1742]

Advertisements

8 responses to “Enigma 1742: Chip-chop

  1. Jim Randell 26 March 2013 at 11:58 pm

    This Python program uses properties of digital roots to narrow down the candidates. It runs in 41ms.

    from itertools import permutations
    from enigma import irange
    
    # consider the number: abcdefg
    # it follows that the digits d, e, f, g are all odd.
    # and also the number abcdefg is not divisible by 3, so its digital root is not divisible by 3
    # bcdefg and abcdef are divisible by 3 (but not 9), digital root is 3 or 6
    # bcdef, bcde, cdef, cde, cd, de, d are not divisible by 3, so nor is their digital root
    
    # compute the digital root of some digits
    def dr(ds):
      n = sum(ds)
      return 0 if n == 0 else 1 + (n - 1) % 9
    
    odd = set((1, 3, 5, 7, 9))
    
    # possible starting digits
    for i in irange(0, 3):
      ds = set(irange(i, i + 6))
      if dr(ds) % 3 == 0: continue
      # choose an odd g, such that the remaining digits have a digital root of 3 or 6
      for g in ds.intersection(odd):
        if dr(ds.difference((g,))) not in (3, 6): continue
        # and choose a, such that the remaining digits have a digital root of 3 or 6
        for a in ds.difference((g, 0)):
          if dr(ds.difference((a,))) not in (3, 6): continue
          # choose the remaining odd digits d, e, f
          for (d, e, f) in permutations(ds.difference((a, g)).intersection(odd), 3):
            # d and de cannot be divisible by 3
            if any(dr(ds) % 3 == 0 for ds in ((d,), (d, e))): continue 
            # choose b, c from what's left
            for (b, c) in permutations(ds.difference((a, d, e, f, g)), 2):
              # cd, cde, cdef, bcde, bcdef cannot be divisible by 3
              if any(dr(ds) % 3 == 0 for ds in ((c, d), (c, d, e), (c, d, e, f), (b, c, d, e), (b, c, d, e, f))): continue
    
              print(a, b, c, d, e, f, g)
    

    Solution: The starting number is 4625317.

    • Naim Uygun 27 March 2013 at 9:48 am

      There is only one answer, in which the middle digit is 5,
      and seven-digit number is divisible by 47.

      • Naim Uygun 27 March 2013 at 10:12 am
        from itertools import permutations
        c=0
        for p in permutations("9876543210",7):
            if p[0]=='0':continue
            s=sorted(p)
            durum=False
            for i in range(1,7):
                if abs(int(s[i])-int(s[i-1]) ) != 1:
                   durum=False
                   break
                durum=True
            if durum==True:
                if int(p[6])%2==0: continue
                     
                y="".join(p)
                l1=y[1:]
               
                r0=y[:-1]
                
                if int(l1)%3 != 0 or int(l1)%9 == 0: continue
                if int(r0)%3 != 0 or  int(r0)%9 == 0: continue
        
                r1=l1[:-1]
                l2=r1[1:]
                r2=l2[:-1]
                l3=r2[1:]
                r3=l3[:-1]
                
                if int(l1)%2==0:continue
                if int(r1)%2==0:continue
                if int(l2)%2==0:continue
                if int(r2)%2==0:continue
                if int(l3)%2==0:continue
                if int(r3)%2==0:continue
                if int(r1)%3==0: continue
                if int(l2)%3==0:continue
                if int(r2)%3==0:continue
                if int(l3)%3==0:continue
                if int(r3)%3==0:continue
                
                     
                x1=r0[:-1]
                x2=x1[1:]
                x3=x2[:-1]
                x4=x3[1:]
                x5=x4[:-1]
                
                if int(x1)%2==0:continue
                if int(x2)%2==0:continue
                if int(x3)%2==0:continue
                if int(x4)%2==0:continue
                       
                if int(x1)%3==0:continue
                if int(x2)%3==0:continue
                if int(x3)%3==0: continue
                if int(x4)%3==0: continue
                if int(x5)%3==0:continue
                
                c=c+1
                print("Answer:",y)
                print(c,")",y,l1,r1,l2,r2,l3,r3)
                print(c,")",y,r0,x1,x2,x3,x4,x5)
        
  2. Brian Gladman 27 March 2013 at 11:22 am

    Here is my version

    from itertools import permutations
    
    test = (False,) * 5 + (True, False)
    
    def cumulative_sum(x):
      it = iter(x)
      s = next(it)
      yield s
      for c in it:
        s = s + c
        yield s
    
    # only bases 1 and 3 give 4 odd numbers from seven total
    for base in (1, 3):
      # with the starting number abcdefg, d, e, f and g are all odd
      for d, e, f, g in permutations(range(base, base + 7, 2)):
        # and d, the last digit left, is not divisible by 3
        if d % 3:
          # a, b and c are all even as we have used all the odd digits
          for a, b, c in permutations(range(base + 1, base + 7, 2)):
            # now compute the sums of the digits in each of the 
            # numbers in the first sequence starting from the
            # last: d, de, cde, cdef, bcdef, bcdefg, abcdefg
            t = tuple(cumulative_sum((d, e, c, f, b, g, a)))
            # and check that only the six digit number is divisible
            # by 3 and not 9 (if a number id divisible by 3 or 9 so
            # is the sum of its digits
            if t[-2] % 9 and tuple(x % 3 == 0 for x in t) == test:
              # now compute the sums of the digits in each of the 
              # numbers in the second sequence starting from the
              # last: d, cd, cde, bcde, bcdef, abcdef, abcdefg
              t = tuple(cumulative_sum((d, c, e, b, f, a, g)))
              if t[-2] % 9 and tuple(x % 3 == 0 for x in t) == test:
                fs = 'The starting number is ' + '{:d}' * 7
                print(fs.format(a, b, c, d, e, f, g))
    
  3. arthurvause 27 March 2013 at 11:58 am

    If the 7 figure number is abcdefg, then as Jim points out, d,e,f,g must be odd, so there are 4 odd digits and 3 even digits, giving two candidate sets for the digits: 1 to 7 and 3 to 9.

    Suppose the set of digits is 1 to 7
    1+..+7 = 1 mod 3 , 1+..+7 = 1 mod 9, so g=7 and a=4 to make the six digit numbers abcdef and bcdefg divisible by 3 and not 9

    Suppose the set of digits is 3 to 9
    3+..+9 = 0 mod 3 and 3+..+9 = 6 mod 9, so a and g have to be 3 and 9 to make abcdef and bcdefg divisible by 3 and not 9, but a has to be even, so 3 to 9 cannot be the set of digits.

    from itertools import permutations as perm
    
    a=4
    g=7
    for (b,c) in perm((2,6)):
      for (d,e,f) in perm((1,3,5)):
        if (b+c+d+e+f)%3 and (c+d+e+f)%3 and (b+c+d+e)%3 \
           and (c+d+e)%3 and (d+e)%3 and (c+d)%3 and (d)%3:
          print a,b,c,d,e,f,g
    
  4. ahmet çetinbudaklar 27 March 2013 at 4:59 pm

    so we have a=4 and g=7 giving us 4261357 , 4261537 , 4263157 , 4263517 , 4265137 , 4265317 , 4625137 , 4625317 , 4623157 , 4623517 , 4621357 and 4621537 out of which only one of them is applicable to the conditions given in the problem, thus giving us the answer.

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: