Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.7K Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

[?] Create examples with Random Integers using Hold, HoldForm, HoldAll?

Posted 6 years ago

If I want to create examples like 3-(-3)+(-5) for young pupils, I have a problem with the brackets since

HoldForm[a - (b) + (c)] 

already removes the brackets. (I'd later replace a,b,c via a -> RandomInteger[{-5, 5}] ) Or to ask even more general, how to create expressions of the form e.g. 3*(-2)+4-(-2) where all integers and calculations, so also *,+,- are chosen randomly? If there's a problem to distinguish 3 and (3), I am happy with (3)*(-2)+(4)-(-2) instead of 3*-2+4--2

Thanks alot!

POSTED BY: Michael Fischer
4 Replies

Hello Michael,

Would it be an option to work with characters instead of numbers?

length = 10;
op0 = {"+", "*"};
op = RandomChoice[op0, length - 1];
numbers = If[# < 0, "(" <> ToString[#] <> ")", ToString[#]] & /@ RandomInteger[{-9, 9}, length];
calc = StringJoin @@ Riffle[numbers, op]
(*  Out:   "4*9+8+(-8)*(-8)+(-9)+(-3)+1*(-6)+5"  *)

EDIT:

Here a little improvement in case you need the results too:

ClearAll["Global`*"]
length = 10;
op0 = {"+", "*"};
op := RandomChoice[op0, length - 1];
numbers := If[# < 0, "(" <> ToString[#] <> ")", ToString[#]] & /@ RandomInteger[{-9, 9}, length];
Table[With[{calc = StringJoin @@ Riffle[numbers, op]}, 
   calc <> " = " <> ToString[ToExpression[calc]]], 20] // Column

If you set the number of lines/calculations big enough you can keep your young pupils busy forever!

POSTED BY: Henrik Schachner

Dear Michael,

to be honest I do not really understand what you mean by "to create fractions instead of "/"", Divide does exactly that. Maybe this in a way might be helpful:

ClearAll["Global`*"]
length = 10;
op0 = {"+", "*", "/"};
op := RandomChoice[op0, length - 1];
numberCandidates = Join[Range[9], -Range[9]]; (* '0' excluded *)    
numbers := If[# < 0, "(" <> ToString[#] <> ")", ToString[#]] & /@ RandomChoice[numberCandidates, length];
TableForm[Table[With[{calc = StringJoin @@ Riffle[numbers, op]},
   {calc <> "   =", ToExpression[calc]}], 10], TableAlignments -> {Right, Left}]

Greetings from Murnau to Vienna!

Henrik

POSTED BY: Henrik Schachner

Dear Henrik, thanks again! I now understand 90% and can apply in this case 100% However, the next problem occurs when I want to create fractions instead of "/". I assume I have to use Divide[a,b] somehow or better Divide@@{a,b} so I can use it on a fitting list like numbers above. To update my question, is the above easily adapted to create

Divide @@ {a, b} + Divide @@ {c, b*d}

where for example in the output bd is evaluated already? Thank you so much already and have a nice weekend! Greetings from Vienna to Weilheim

POSTED BY: Michael Fischer

Dear Henrik, thank you a lot! Luckily they are not my pupils, it started with the children of my neighbors who need a little bit support and it will probably end with an "exercise"-Donation for the open project Lama, [1], a project for teachers in Austria. Thanks also for the EDIT. It would have been my next question since obviously solutions are great for the children. I'll play around with the code, I consider myself not a newbie in Mathematica but when it comes to #, @ and @@ I start struggling.

[1] https://mylama.github.io/lama/

POSTED BY: Michael Fischer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard