2
$\begingroup$

How do I make Mathematica show me the solutions that it has found while it is searching for them, specifically in this:

Solve[
  {{(n^2) (s - 2) - (n) (s - 4)}/2 == 2^p - 1, 2 < n < 2100,0 < p < 10, 9 < s < 21}, 
  {n, p, s}, Integers]

The ranges can/will be adjusted, I just want to set it so that I can see the progress that Mathematica is making.

$\endgroup$
1
  • 1
    $\begingroup$ The short answer is it can't be done. Longer answers will probably be a variation on that, maybe with salad dressing added. $\endgroup$ Dec 3, 2015 at 19:46

1 Answer 1

5
$\begingroup$

With a single Solve command, I think this is impossible. But you can do something like the following. There are some strange curly brackets in your equation, so I hope I repaired it correctly. (I did not find a solution, and FindInstance did not give a solution either.)

Monitor[Table[{p,s, sol=Solve[(n^2 (s-2)-n (s-4))/2==2^p-1 && 2<n<2100, n,Integers] }, {p,1,9},{s, 10, 20}], {p,s,sol}]

Monitoring does not show much here, because the result turns up almost immediately.

Instead of Monitor, you can use a Print command inside the Table:

Table[sol=Solve[(n^2 (s-2)-n (s-4))/2==2^p-1 && 2<n<2100, n,Integers];
  Print[{p,s,sol}]; {p,s,sol}, {p,1,9},{s,10, 20}]

If you do not want to see the empty results, you can do something like

result = {};
Do[sol = Solve[(n^2 (s - 2) - n (s - 4))/2 == 2^p - 1 && 2 < n < 2100,
   n, Integers];
   If[sol != {}, Print[{p, s, sol}]; 
   AppendTo[result, {p, s, sol}]], {p, 1, 9}, {s, 10, 20}];
result

You might place the Do-loop in a Monitor, replace the Print statement with qq={p,s,sol} and use qq in the second argument of Monitor.

$\endgroup$
3
  • $\begingroup$ Thank you for the answer, it was just what I needed, just one extra small question, when using the Monitor command, the output shows all of the results, not just the ones for which an $n$ has been found. Any way to fix this? $\endgroup$ Dec 3, 2015 at 19:57
  • $\begingroup$ @redelectrons I added a few lines to my answer. $\endgroup$ Dec 3, 2015 at 20:21
  • $\begingroup$ It worked, many thanks $\endgroup$ Dec 3, 2015 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.