[Wellington-pm] SEND + MORE = MONEY

Grant McLean grant at mclean.net.nz
Tue Mar 1 10:10:54 PST 2005


Can anyone come up with creative solutions for this?


Given the equation:

  SEND
 +MORE
 =====
 MONEY

Where each letter represents a digit (0-9) and neither S nor M can be
zero.  Find the values for the letters which satisfy the equation.
There is only one solution.


I came up with a brute force approach (below) which takes nearly two
minutes (1:50) on my workstation to check all possible answers and prove
the assertion that there is only one answer.

Cheers
Grant



Spoiler follows ...





















































#!/usr/bin/perl -w

use strict;

my $message = 'SEND + MORE = MONEY';

my @letters = sort keys %{{ map { $_, $_} ($message =~ /([A-Z])/g) }};

print "$message\n";
solve($_) foreach (0..9);

sub solve {
  my %d = map { $_, undef} (0..9);
  delete $d{$_} foreach(@_);
  
  if(@_ == @letters) {
    my $tr = join('', 'tr/', @letters, '/', @_, '/');
    $_ = $message;
    eval "$tr";
    return if(/\b0/);
    if(/^(....)...(....)...(.....)/) {
      print "$_\n" if($1 + $2 == $3);
    }
    return;
  }

  solve(@_, $_) foreach keys %d;
}





More information about the Wellington-pm mailing list