Enigmatic Code

Programming Enigma Puzzles

Enigma 1544: Spanish squares

From New Scientist #2707, 9th May 2009 [link]

Cero, uno and nueve are the Spanish words for 0, 1 and 9, so it is appropriate that I can make the following statement:

CERO, UNO and NUEVE are perfect squares.

In this statement digits have been consistently replaced by capital letters, different letters being used for different digits.

Please send in the numerical value of the square root of (CERO × UNO × NUEVE).

[enigma1544]

Advertisements

2 responses to “Enigma 1544: Spanish squares

  1. Jim Randell 8 April 2012 at 10:07 pm

    Here’s my original Perl code. It runs in 26ms.

    use Enigma qw/distinct square/;
    
    my ($C, $E, $R, $O, $U, $N, $V);
    my ($CERO, $UNO, $NUEVE);
    my $S;
    
    for $U (1..9) {
      for $N (1..9) {
        next unless distinct($N, $U);
        for $O (0..9) {
          next unless distinct($O, $U, $N);
          $UNO = int("$U$N$O");
          next unless square($UNO);
    
          for $C (1..9) {
            next unless distinct($C, $U, $N, $O);
            for $E (0..9) {
              next unless distinct($E, $U, $N, $O, $C);
              for $R (0..9) {
                next unless distinct($R, $U, $N, $O, $C, $E);
                $CERO = int("$C$E$R$O");
                next unless square($CERO);
    
                for $V (0..9) {
                  next unless distinct($V, $U, $N, $O, $C, $E, $R);
                  $NUEVE = int("$N$U$E$V$E");
                  next unless square($NUEVE);
    
                  $S = sqrt($CERO * $UNO * $NUEVE);
                  print "CERO=$CERO UNO=$UNO NUEVE=$NUEVE S=$S\n";
                }
              }
            }
          }
        }
      }
    }
    

    Solution: The square root is 812224.

    • Jim Randell 8 April 2012 at 10:10 pm

      And here it is in Python. This runs in 50ms.

      from itertools import permutations
      from enigma import concat, is_square, printf
      
      ds = set(range(10))
      
      for (U, N, O) in permutations(ds, 3):
        if 0 in (U, N): continue
        UNO = int(concat(U, N, O))
        if not is_square(UNO): continue
      
        d1 = ds.difference((U, N, O))
        for (C, E, R) in permutations(d1, 3):
          if C == 0: continue
          CERO = int(concat(C, E, R, O))
          if not is_square(CERO): continue
      
          d2 = d1.difference((C, E, R))
          for V in d2:
            NUEVE = int(concat(N, U, E, V, E))
            if not is_square(NUEVE): continue
      
            r = is_square(CERO * UNO * NUEVE)
            if not r: continue
      
            printf("r={r} [UNO={UNO} CERO={CERO} NUEVE={NUEVE}]")
      

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: