Math Genome Fun

You have thirteen characters: the numbers 1-9 and the operators for Plus, Minus, Divide and Multiply. Arrange them in a string so they have the highest possible value, or write a program to do this for you. If a string cannot execute as a mathematical function, it scores zero.

 

Full list [1,2,3,4,5,6,7,8,9,+,-,*,/]

There are 13! possibilities. How many score more than 0 (are mathematically viable)? How many steps did you program take / candidates did it evaluate. How did you know when to stop?

207 thoughts on “Math Genome Fun

  1. Richardthughes: can you expand on that a bit?

    does 3*4-5 =(3*4)-5 or 3*(4-5)

    programming languages have default ways of deciding which operations to do first. Where the implied parentheses go.

    Or maybe I’m just confused. Wouldn’t be the first time.

  2. Can we use regular expressions? I guess that would defeat the purpose of measuring the steps of a program

  3. At like at 13! (6 billion ish) possibilities you could brute force it quite quickly using nested loops. I’d like to explore method evaluation as part of this, I think.

  4. Richardthughes: Curse you, lack of edit. This place is waaaaaaay worse than UD!

    For once we agree on something. We used to be able to edit our OP’s until petrushka went and did the naughty and everyone else got punished for it. Talk about admins over-reacting. That would never happen at UD.

  5. Should we allow + and – to denote positive and negative? Should +9876*54/-321 be allowed? I’m thinking… not.

  6. Using magic presuppositionalism I’d guess something like

    975*864+3/1-2

    I’m somewhat flummoxed as to how to solve it or how to write a GA to solve it.

  7. Man, I had to read that OP a few times to make sense of it. I’m invoking the Harshman rule.

    So we have to use all the digits and all the operators and we can only use each particular digit and each particular operator once, is that correct? Couldn’t you toss in a zero just to make it interesting?

    So for my divide I’m going to want to divide by 1. And for my subtract I think I am going to want to subtract 2. Just thinking intuitively. And I am going to want to multiply the two highest numbers.

    My question is, how do we know the correct answer?

    Also, if there’s only one answer then you can’t use a GA to solve it.

  8. Mung: So we have to use all the digits and all the operators and we can only use each particular digit and each particular operator once, is that correct? Couldn’t you toss in a zero just to make it interesting?

    Correct. I removed 0 to exclude infinity and Joe Gallien’s incorrect slobberings that would accompany it.

    Mung: So for my divide I’m going to want to divide by 1. And for my subtract I think I am going to want to subtract 2. Just thinking intuitively. And I am going to want to multiply the two highest numbers.

    That’s some good thinking (I think).

    Mung: My question is, how do we know the correct answer?

    We could run through all 6ish Billion. Not really that hard for a modern PC

    Mung: Also, if there’s only one answer then you can’t use a GA to solve it.

    What makes you say that?

  9. Richardthughes: What makes you say that?

    Stupid comments by ignorant people over in the turbines thread. Do you just have them on ignore? You should. For example, the Dawkins weasel program isn’t a GA because it has a single target. That kind of nonsense.

    It was sarcasm.

    Seriously, did you just miss all my posts in that thread asking just how many targets were needed before something could be a GA?

  10. Mung,

    I think there’s some confused over how explicit a target should be and if local or global maximas are required. If evolution is a search, has it found it’s target?

  11. Neat thing about the Ruby language is that you can evaluate strings as if they were code.

    D:\projects>ruby -e ‘puts eval(“2+2”)’
    4

    But “7*/1” could be a problem.

  12. Mung: Also, if there’s only one answer then you can’t use a GA to solve it.

    Actually as AxB = BxA, I’m thinking there’s more than one answer.

  13. I think we should all agree that this problem is beyond the capabilities of genetic algorithms. We should all presuppose that only an intelligent designer can intuitively see the quickest path to solution.

    Evolution is dead.

  14. Mung: But “7*/1” could be a problem.

    catch that as an error and give it a zero. Good for you for coding!

  15. So in a program we’d want to create all possible combinations, I swear I wrote an algorithm just the other day to do that. =p

    We’d want to do that so we could be certain we had the highest possible value.

    Discard any string with a lower value than the current highest value. Store any strings with equal value so we could see if there is more than one solution.

    A GA could not tell us that, though, not in a way that wee could be sure of. Do you agree?

  16. Richardthughes: catch that as an error and give it a zero. Good for you for coding!

    Yes. It will raise an exception and on any exception we can just return a value of zero.

    But the number of steps in the program is going to always be the same, wouldn’t you agree?

  17. Mung: A GA could not tell us that, though, not in a way that wee could be sure of. Do you agree?

    There’s no guarantee that a GA finds a global maxima. It becomes more likely with more time, but I believe you are correct (how many rolls to guarantee a 6) etc. That being said, we could test its efficacy over N trials vs. exhaustive enumeration, which guarantees us a maximum as it tests all possibilities,

  18. Here’s what I came up with for the size of the sequence space:

    302,875,106,592,253

    Do you concur?

  19. Mung: I absolutely agree with you about that. The order doesn’t matter there.

    You are totally sciencing, Mung! Where’s KeithS? KeithS! Get in here!

  20. I think I know where you’re coming from now Rich. This is utter brilliance. A GA should be the fastest way to get to the solution, but it would be impossible to know what that solution is until the entire search space is explored.

    Or maybe I need to think about this a bit more…

  21. Richardthughes: I think it’s 13! = 6,227,020,800. Are we collaborating, Mung?

    God Forbid! LoL.

    Well, I was using (length of sequence) ^ (number of characters). In this case they are both equal. I admit this is a carry-over of a discussion of amino acid sequence space and I was using a program developed for that so it may not apply here. It might be interesting to explore the differences.

  22. Real code:

    ALPHABET = %w[1 2 3 4 5 6 7 8 9 * / + -]
    NUMBER_OF_SYMBOLS = ALPHABET.length
    LENGTH_OF_SEQUENCE = 13
    SEQUENCE_SPACE = NUMBER_OF_SYMBOLS ** LENGTH_OF_SEQUENCE

    Gawd. Did I just pull a Matzke?

  23. ALPHABET.each do |symbol|
    beta = ALPHABET – [symbol]
    # create all possible configurations of beta
    # perhaps through a sequence of swaps/shifts
    end

    So now I have an array that contains all the other “letters” contained in the “alphabet” and I need to iterate over all possible unique arrangements of those remaining “letters.” Suggestions for an algorithm?

    OOP would be to create a new type of object/class that accepts an array and which implements an each method which iterates over all possible configurations. Wonder if there’s something like that in the available libraries.

  24. Here’s how to go about calculating the number of “legal” sequences.

    Assuming the operators are all binary, every legal sequence must look like this:

    num0 op0 num1 op1 num2 op2 num3 op3 num4

    Decompose that into two problems:

    1. How many ways can the digits 1-9 be arranged into 5 distinct numbers?
    2. How many distinct sequences can be formed from the operators +,-,x,/ ? (where a sequence is op0 op1 op2 op3)

    Multiply the two answers and you’ll have the number of legal sequences.

  25. dazz, the number of symbols is not typically the same as the length of the sequence. Imagine a world where all protein sequences are restricted to chains of 20 or fewer amino acids.

Leave a Reply