In essence what one wants to do is to allocate the numbers 0 - 9 over 10 or less letters. To make it easier we will allocate the figures 0 to 9 over exactly 10 letters (we will make up the missing letters and drop them when appropriate).

Of course we may allocate each letter only once, but I think that using a clever algorithm to make sure that each letter is not used more than once will take more time than using real brute force and dropping "wrong" solutions afterwards.

So I would set up a for loop starting with '0123456789' till '9876543210' with a step of 1 and directly allocating the first digit to the (alphabetically) first letter used in your equation and so on. (Perhaps it is easier to start with the last digit in order not to involuntarily shift the whole pattern if it starts with a zero)

Then checking if the equation holds and if it holds checking if no digit has been used more than once.

Thus you delay at lot of "expensive" tests and logic until they are really necessary.

If you optimise the allocation of the digits to the letters and you hard code the testing of the equation, it should be possible.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law


In reply to Re: Words that equal numbers by CountZero
in thread Words that equal numbers by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.