Enigmatic Code

Programming Enigma Puzzles

Enigma 1117: Reapply as necessary

From New Scientist #2273, 13th January 2001 [link]

Recently I read this exercise in a school book:

“Start with a whole number, reverse it and then add the two together to get a new number. Repeat the process until you have a palindrome. For example, starting with 263 gives:

leading to the palindrome 2662.”

I tried this by starting with a three-figure number. I reversed it to give a larger number, and then I added the two together, but my answer was still not palindromic. So I repeated the process, which gave me another three-figure number which was still not palindromic. In fact I had to repeat the process twice more before I reached a palindrome.

What number did I start with?

[enigma1117]

Advertisements

3 responses to “Enigma 1117: Reapply as necessary

  1. Jim Randell 1 May 2017 at 8:19 am

    This Python program runs in 55ms.

    from enigma import irange, nreverse, printf
    
    # apply the process (fewer than M times)
    # return the intermediate numbers, ending with the palindrome
    def process(n, M):
      s = [ n ]
      while not(len(s) > M):
        # is the number palindromic?
        r = nreverse(n)
        if n == r: return s
        # if not repeat with the sum of the number and its reverse
        n += r
        if n in s: break
        s.append(n)
      return None
    
    # check the given example
    assert process(263, 4) == [ 263, 625, 1151, 2662 ]
    
    # consider 3 digit numbers
    for n in irange(100, 999):
    
      # the reverse of the first number must be larger
      r = nreverse(n)
      if not(r > n): continue
    
      # and we have 3 more steps to get the palindrome
      s = process(n + r, 4)
      if s is None or len(s) != 4: continue
    
      # and the result of the second application is 3 digit
      if 99 < s[1] < 1000:
        printf("{n} -> {s}")
    

    Solution: The starting number was 192.

    192 → 483 → 867 → 1635 → 6996

    Most 3-digit numbers resolve quite quickly to a palindromic number, but there are a few that take 10 or more applications of the procedure:

    10: 829
    11: 167, 266, 365, 860
    14: 849
    15: 177, 276, 375, 870
    17: 739, 899
    22: 869
    23: 187, 286, 385, 880

    ?: 196, 295, 394, 689, 788, 790, 879

    The ones marked with a ? never reach a palindromic number.

    For numbers not ending in 0 the reverse of that number will also behave in the same way (e.g. 928 will also take 10 applications of the procedure before reaching a palindrome).

  2. geoffrounce 2 May 2017 at 11:07 am

    Jim, I used your ‘nreverse’ function from enigma.py to easily reverse numbers in my programme.

    import time
    start = time.time()
    
    from enigma import nreverse
    
    for n1 in range(100,1000):
        if nreverse(n1) <= n1: continue
        s1 = n1 + nreverse(n1)
        # 1st try is not a palindrome
        if str(s1) != str(s1)[::-1]:   
            s2 = s1 + nreverse(s1)
            
            # 2nd try is not a palindrome of 3-digit length
            if str(s2) != str(s2)[::-1] and len(str(s2))== 3:
                s3 = s2 + nreverse(s2)
                
                # 3rd try not a palindrome
                if str(s3) != str(s3)[::-1]:   
                    s4 = s3 + nreverse(s3)
                    
                    # 4th try is a palindrome
                    if str(s4) == str(s4)[::-1]:
                        print( 'Sequence =',(n1, nreverse(n1), \
                                s1, s2, s3, s4), end = '\n')
                        print(time.time() - start)   # 31 msec
    
    # Sequence = (192, 291, 483, 867, 1635, 6996)
    

    Without the constraint that the second addition must be three digits, the first number is 174
    and there are multiple answers.

    Output – 2nd addition not restricted to three digits
    (174, 471, 645, 1191, 3102, 5115)
    (175, 571, 746, 1393, 5324, 9559)
    (183, 381, 564, 1029, 10230, 13431)
    (192, 291, 483, 867, 1635, 6996) << answer
    (195, 591, 786, 1473, 5214, 9339)
    (273, 372, 645, 1191, 3102, 5115)
    (274, 472, 746, 1393, 5324, 9559)
    (294, 492, 786, 1473, 5214, 9339)
    etc

    • geoffrounce 3 May 2017 at 7:29 pm
      % A Solution in MiniZinc
      % Sequence : ABC + CBA = DEF + FED = GHI + IHG = JKLM + MLKJ = NPQR
      % After the second 3-digit sum, 4-digit integers assumed
      
      include "globals.mzn";
       
      % Digits in the sums
      var 1..9:A;   var 1..9:B;  var 1..9:C;  var 1..9:D;
      var 1..9:E;   var 1..9:F;  var 1..9:G;  var 1..9:H;
      var 1..9:I;   var 1..9:J;  var 1..9:K;  var 1..9:L;
      var 1..9:M;   var 1..9:N;  var 1..9:P;  var 1..9:Q;  
      var 1..9:R; 
      
      % Intermediate sum values
      var 100..999: ABC = 100*A + 10*B + C;
      var 100..999: CBA = 100*C + 10*B + A;
      var 100..999: DEF = 100*D + 10*E + F;
      var 100..999: FED = 100*F + 10*E + D;
      var 100..999: GHI = 100*G + 10*H + I;
      var 100..999: IHG = 100*I + 10*H + G;
      var 1000..9999: JKLM = 1000*J + 100*K + 10*L + M;
      var 1000..9999: MLKJ = 1000*M + 100*L + 10*K + J;
      var 1000..9999: NPQR = 1000*N + 100*P + 10*Q + R;
      
      % Main addition sum 
      constraint CBA > ABC /\ ABC + CBA == DEF /\ DEF + FED == GHI
      /\ GHI + IHG == JKLM /\ JKLM + MLKJ == NPQR;
      
      % ABC, DEF and GHI are not 3-digit palindromes
      constraint A != C /\ F != D /\ G != I  
      /\ J != M  /\ K != L   %  JKLM is a 4-digit non-palindrome 
      /\ N == R /\ P == Q;   %  NPQR is a 4-digit palindrome
      
      solve satisfy;
      
      output ["Sequence is : " ++ show(ABC) ++ ", " ++ show(DEF) ++ ", " 
      ++ show(GHI) ++ ", " ++ show(JKLM) ++ " and " ++ show(NPQR)];
      
      % Sequence is : 192, 483, 867, 1635 and 6996
      % Finished in 132msec
      

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: