3
$\begingroup$

I use the Reduce function solve some problem, such as

Reduce[2k + 1 > 100 && k < 200, k, Integers]//ToString

How can I only get the result like k = 1, k = 2...... instead of the format such as C[1] ∈ Integers && 2831. <= C[1] <= 14999. && k == 1 + 2 C[1].

$\endgroup$
1

2 Answers 2

5
$\begingroup$

When I try your example I do not get the same result you say you get.

Try

StringReplace[ToString[Flatten[ReplaceAll[
  FindInstance[2k+1>100&&k<200,k,Integers,200],
  Rule->Equal]]],"=="->"="]

which instantly returns

{k = 50, k = 51, k = 52, k = 53, k = 54, k = 55, k = 56, k = 57, k = 58,
 k = 59, k = 60, k = 61, k = 62, k = 63, k = 64, k = 65, k = 66, k = 67,
 k = 68, k = 69, k = 70, k = 71, k = 72, k = 73, k = 74, k = 75, k = 76,
 k = 77, k = 78, k = 79, k = 80, k = 81, k = 82, k = 83, k = 84, k = 85,
 k = 86, k = 87, k = 88, k = 89, k = 90, k = 91, k = 92, k = 93, k = 94,
 k = 95, k = 96, k = 97, k = 98, k = 99, k = 100, k = 101, k = 102, k = 103,
 k = 104, k = 105, k = 106, k = 107, k = 108, k = 109, k = 110, k = 111,
 k = 112, k = 113, k = 114, k = 115, k = 116, k = 117, k = 118, k = 119,
 k = 120, k = 121, k = 122, k = 123, k = 124, k = 125, k = 126, k = 127,
 k = 128, k = 129, k = 130, k = 131, k = 132, k = 133, k = 134, k = 135,
 k = 136, k = 137, k = 138, k = 139, k = 140, k = 141, k = 142, k = 143,
 k = 144, k = 145, k = 146, k = 147, k = 148, k = 149, k = 150, k = 151,
 k = 152, k = 153, k = 154, k = 155, k = 156, k = 157, k = 158, k = 159,
 k = 160, k = 161, k = 162, k = 163, k = 164, k = 165, k = 166, k = 167,
 k = 168, k = 169, k = 170, k = 171, k = 172, k = 173, k = 174, k = 175,
 k = 176, k = 177, k = 178, k = 179, k = 180, k = 181, k = 182, k = 183,
 k = 184, k = 185, k = 186, k = 187, k = 188, k = 189, k = 190, k = 191,
 k = 192, k = 193, k = 194, k = 195, k = 196, k = 197, k = 198, k = 199}

as one long string. See if that works for you. If this isn't what you are looking for then try to explain what you need and I will see if I can fix this for you.

Look up each of those functions in the help system to understand exactly how and why this works

$\endgroup$
4
  • $\begingroup$ You could use Apply[List, ...] to have it strictly be a list, correct? Or, perhaps it starts as a List, does it not? I like this answer a lot, but I think OP was hoping for the List outcome, even if this method might be better. Oh, hah, you literally just edited it. I like the usefulness of Apply[Or, ...] though, too. $\endgroup$ Jun 9, 2020 at 2:14
  • 1
    $\begingroup$ @CATrevillian There are always a dozen different ways to do anything in Mathematica. We can all try to guess what he really wanted. I'm not exactly certain what he really wants from this. Because he shows getting a string out of this I'm wondering if he is going to take that and paste it into something else. Hopefully something in this will help him. $\endgroup$
    – Bill
    Jun 9, 2020 at 2:23
  • $\begingroup$ that is one of my favorite things about Mathematica! I have found myself taking a function I want to learn about, then using it to do as many different tasks as I can! That usually means that there’s another function that just does what I am trying to do, but that takes the fun out of it hah! $\endgroup$ Jun 9, 2020 at 2:45
  • 1
    $\begingroup$ @CATrevillian Maybe you could turn that idea into a service. Once a week subscribers would get an email containing one odd MMA function along with a dozen astonishing ways to use that which you would never imagine, but actual practical uses, not just nonsense. Maybe include a few quiz questions at the end asking how to use that function, graded from fairly easy to completely mind boggling, making you think and expand your skill. I'd subscribe. Sign me up. $\endgroup$
    – Bill
    Jun 9, 2020 at 3:25
5
$\begingroup$

Update: The system option (see Original answer below) can be set temporarily via the Method option of Reduce to get an enumeration of the solutions:

Reduce[2 k + 1 > 100 && k < 200, k, Integers, 
 Method -> {"DiscreteSolutionBound" -> 150}]

Original answer: Set "DiscreteSolutionBound" larger than the number of solutions:

SetSystemOptions["ReduceOptions" -> {"DiscreteSolutionBound" -> 150}]

Reduce[2 k + 1 > 100 && k < 200, k, Integers]

(*  k == 50 || ...|| k == 199  *)

(To reset: SetSystemOptions["ReduceOptions" -> {"DiscreteSolutionBound" -> 10}].)

You can also get the solutions from Solve without set the system option:

sol = Solve[2 k + 1 > 100 && k < 200, k, Integers]

(*  {{k -> 50}, ..., {k -> 199}}  *)

Update: Solve has a bound, "SolveDiscreteSolutionBound" -> 1000000, which can be set, if needed, via either of the following:

Solve[..., Method -> {"SolveDiscreteSolutionBound" -> n}]
SetSystemOptions["ReduceOptions" -> {"SolveDiscreteSolutionBound" -> n}]

Addendum: To get a string like the OP indicates, one might do the following:

sol /. Rule -> (HoldForm[Set[##]] &) //
 Flatten //
 ToString // 
 StringTrim[#, "{" | "}"] &
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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