Enigmatic Code

Programming Enigma Puzzles

Enigma 1557: Reverse division

From New Scientist #2720, 8th August 2009 [link]

1. What is the four-digit integer divisible by 11 and 13 whose reverse is a larger integer also divisible by 11 and 13?

2. What is the four-digit integer divisible by 17 and 19 whose reverse is a larger integer also divisible by 17 and 19?

Neither of your answers may have a leading zero.

[enigma1557]

Advertisements

2 responses to “Enigma 1557: Reverse division

  1. Jim Randell 5 March 2012 at 10:45 pm

    The following Python program runs in 34ms.

    from enigma import lcm, irange, divc, divf, nreverse, printf
    
    # generic solution
    def solve(n1, n2):
      # find the 4 digit multiples of m
      m = lcm(n1, n2)
      for i in irange(divc(1000, m), divf(9999, m)):
        n = i * m # n is a multiple of n1 and n2
        if n % 10 == 0: continue # must not be divisible by 10
        r = nreverse(n) # reverse it
        if not(r > n): continue # r must be greater
        if r % m > 0: continue # and also divisible by n1 and n2
        printf("[{n2},{n1}] {n} -> {r}")
    
    solve(11, 13)
    solve(17, 19)
    

    Solution: (1). 1859; (2). 3876.

  2. geoffrounce 14 August 2016 at 9:19 am
    % A solution in MiniZinc
    include "globals.mzn"; 
    
    var 0..9: a; var 0..9: b; var 0..9: c; var 0..9: d;
    var 0..9: e; var 0..9: f; var 0..9: g; var 0..9: h;
    
    var 1000..9999: abcd = 1000*a + 100*b + 10*c + d;
    var 1000..9999: dcba = 1000*d + 100*c + 10*b + a;
    
    var 1000..9999: efgh = 1000*e + 100*f + 10*g + h;
    var 1000..9999: hgfe = 1000*h + 100*g + 10*f + e;
      
    constraint a > 0 /\ d > 0 /\ e > 0 /\ h > 0;
    
    % Part 1
    constraint abcd mod 11 == 0 /\ abcd mod 13 == 0
            /\ dcba mod 11 == 0 /\ dcba mod 13 == 0
            /\ dcba > abcd;
    
    % Part 2
    constraint efgh mod 17 == 0 /\ efgh mod 19 == 0
            /\ hgfe mod 17 == 0 /\ hgfe mod 19 == 0
            /\ hgfe > efgh;
    
    solve satisfy;
    
    output ["1st Number is "++ show(abcd) ++ "\n" ++
            "2nd Number is "++ show(efgh)];
    
    % 1st Number is 1859
    % 2nd Number is 3876
    % Finished in 89msec
    %
    

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: