How about this, based on enumerating binary trees: e←{⍵,⊂f ⍵} f←{↑,/(⊂⍵)g¨⍳≢⍵} g←{↑,/,(⍵⊃⍺)∘.h ⍵⊃⌽⍺} h←{(⊂'(',⍺),¨'+-×÷',¨⊂(⍵,')')} t1←,⊂,'4' ⍝ vec of charvecs representing trees of size 1 t←,⊂t1 ⋄ t←e⍣4⊢t (t1 t2 t3 t4 t5)←t
Now t5 is a list of parenthesised expressions of "size" 5, i.e. containing 5 4s. At this point in Dyalog APL I would do: ⊃t5/⍨7={11::0 ⋄ ⍎⍵}¨t5 ⍝ 11::0 means return 0 if ⍎ signalled DOMAIN ERROR (4+((4+(4+4))÷4)) (4-((4-(4×4))÷4)) (4+(((4+4)+4)÷4)) (4+(((4×4)-4)÷4)) (((4+(4+4))÷4)+4) (((4×(4+4))-4)÷4) ((((4+4)+4)÷4)+4) ((((4+4)×4)-4)÷4) ((((4×4)-4)÷4)+4) I am sure there is a simple equivalent in GNU APL. For bonus marks I guess you might want to remove from this list any expressions that are equivalent due to + and × being commutative and associative. Jay. On 24 January 2017 at 15:47, Elias Mårtenson <loke...@gmail.com> wrote: > My daughter had this maths problem, and quote it from memory: > > Given five 4's, separated by the mathematical functions +, -, × and ÷ form > the number 7. The problem also allowed her to use parentheses. > > I decided to write an APL expression to solve this, and this is what I came > up with: > > d ← {⍵÷⍺} > (↑¨ 7 = ⍎¨ v) / v ← {"4 " , ↑,/ {{⍵," 4 "}¨⍵} ⍵}¨ ,↑∘.,/4 ⍴ ⊂'+-×d' > > Now, this program finds two solutions, which is good enough for me. However, > my program doesn't find solutions that require parentheses. > > Problem 1: Can anyone simplify my program? > Problem 2: Can anyone come up with a solution that also checks for > parentheses? > > Regards, > Elias