Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2015
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. binaryTrees[0] = {};
  2. binaryTrees[1] = {leaf};
  3. binaryTrees[n_Integer] := Flatten @ Table[vtx[t1, t2],
  4. {m, 1, n-1}, {t1, rootedBinaryTrees[n-m]}, {t2, rootedBinaryTrees[m]}];
  5.  
  6. enumeratedTrees = Block[{i = 1}, # /. leaf :> i++] & /@ binaryTrees[9];
  7. operatorPermutations = Tuples[Inactive /@ {Plus, Subtract, Times, Divide}, 8];
  8. solutions = {};
  9.  
  10. Do[
  11. s = Select[
  12. Table[Block[{i = 1},
  13. tree /. vtx :> operatorPermutations[[m, i++]]],
  14. {tree, enumeratedTrees}],
  15. Activate[#] == 2015 &];
  16. If[Length[s] > 0, Print[s]; AppendTo[solutions, {m, s}]];
  17. If[Mod[m, 500] == 0, Print[m]],
  18. {m, 65536}];
  19. Export["solutions_raw.txt", solutions];
  20.  
  21. stringify[Inactive[head_][a_, b_]] := ToString@StringForm["(`` `` ``)",
  22. stringify[a],
  23. head /. {Plus -> "+", Subtract -> "-", Times -> "*",
  24. Divide -> "/"},
  25. stringify[b]];
  26. stringify[n_Integer] := ToString[n];
  27.  
  28. Export["solutions_formatted.txt",
  29. stringify /@ Flatten[Last /@ solutions]];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement