Advertisement
Guest User

mathematica Solving a system of equations with constraints..

a guest
May 19th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. Clear[a, b, c, d, e, f, g, h];
  2. sol = {a, b, c, d, e, f, g, h} /. Solve[{
  3. (*Max[{a,b,c,d,e,f,g,h}]\[Equal]14,*)
  4. (*Min[{a,b,c,d,e,f,g,h}]\[Equal]1,*)
  5. Total[{a, b, c, d, e, f, g, h} -
  6. Sort[{a, b, c, d, e, f, g, h}]] == 0,
  7. Abs[Total[{a, b, c, d}] - Total[{e, f, g, h}]] ==
  8. 16, (*48 value row5 has 192 for this,
  9. 480 value row6 has 3840 for this,
  10. 5760 value row7 has 552960 for this, 16,16*12,16*12*20,
  11. 16*12*20*12^2*)
  12. (*h\[Equal]4,(*last value hard coded constraint*)*)
  13. (*g\[Equal]13,(*penultimate value hard coded constraint,
  14. since have formula for the penultimate value*)*)
  15. g > 0,
  16. (*
  17. a+e+c+g\[Equal]32,(*32+28=
  18. 60 is from the sequence with row length 1,3,15,45,45,405*)
  19. b+f+d+h\[Equal]28,
  20. *)
  21. (*
  22. (a+e+c+g)-(b+f+d+h)\[Equal]4,
  23. *)
  24. a + h == 15,
  25. b + g == 15,
  26. c + f == 15,
  27. d + e == 15,
  28. a + b + c + d + e + f + g + h == 60,
  29. a - b == g - h,
  30. b - c == f - g,
  31. c - d == e - f,
  32. d - e == b - c,(*additional middle value formula,
  33. not sure if valid for larger rows*)
  34. d - e == f - g,(*additional middle value formula,
  35. not sure if valid for larger rows*)
  36. a - b + b - c + c - d + d - e + e - f + f - g + g - h == 7,
  37. b - a + c - b + d - c + e - d + f - e + g - f + h -
  38. g == -7,(*some of these constraints are redundant but ok to \
  39. leave them in*)
  40. (*
  41. Mod[c,2]>0,
  42. Mod[d,2]\[Equal]0,
  43. *)
  44. Total[Abs[{a - b, b - c, c - d, d - e, e - f, f - g, g - h}]] == 35
  45. },
  46. {a, b, c, d, e, f, g, h}, Integers]
  47.  
  48. sol2 = Select[
  49. sol, ({a, b, c, d, e, f, g, h} = #;
  50. Total[Abs[{a - b, b - c, c - d, d - e, e - f, f - g, g - h}]] ==
  51. 35) &]
  52.  
  53. sol3 = Select[
  54. sol2, ({a, b, c, d, e, f, g, h} = #;
  55. Mod[b, 2] == 0 && Mod[c, 3] > 0 (*&&etc*)) &]
  56.  
  57. Select[sol3, ({a, b, c, d, e, f, g, h} = #;
  58. Abs[Total[{a, b, c, d}] - Total[{e, f, g, h}]] == 16) &]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement