0

Warning: Completely new to Stack Overflow, so any tips on asking questions so I can help the people answering are appreciated.

I've come to the conclusion that I need to factorize, decompose, or implement some type of branching process to get the results I want. Please correct me if I am wrong, I am here to learn.

I've sourced that from two different StackOverflow answers.

Decompose integers larger than 100 digits

https://softwareengineering.stackexchange.com/questions/195813/generating-random-math-expression

I am working on a problem but I don't have the math background to completely understand the appropriate solution. So more than just an answer, but any reference to skills I can pick up to learn myself would be awesome.

What I am expecting is an algorithm which I can use to generate the results.

So here is what I want to happen.

Say I have a integer, 56.

I want to be able to generate a random math expression of a finite size which equals 56. This expression should be limited to 4 operators; Add, Subtract, Multiply, Divide.

So the equation I expect as a result of this algorithm is;

Input: integer 56, length 4

Results: 10 + 2 x 3 + 20 = 56

another example

Input: integer 34, length 5

3 * 7 / 2 * 3 + 4 = 34

Some notes I should fill you in on. I want to be able to generate these random expressions from an integer I provide, at a length I determine. The order of operators aren't important. I don't want to generate an equation without knowing the results randomly as I need to know the results prior to execution for efficiency. You will also notice in the second example, if the whole numbers are treated as floats, the equation would be incorrect. I decidedly want the individual operations to floor results, or round to the previous integer rather than introduce a decimal point. I am not 100% I want to round down, but let me know your thoughts on handling rounding.

I have a few ideas of how to do this myself, but I really would like second eyes on this to understand this problem on an academic level.

If it isn't already obvious, I am self taught. Thanks for your answers! I hope to be apart of this amazing community.

Community
  • 1
  • 1

1 Answers1

0

It seems as if you ignore operator precedence. Then, you can simply generate the formula from the back.

I.e. start with your result (e.g. 34). Then choose any operator and a suitable second operand (e.g. +4). This leads you to the problem of finding a formula that results in 30 with length one shorter than the original length. Do this until you reach length 0.

Choosing an operator can be done randomly. But if you want to stay in a certain range, you might also want to consider the possible operands. For division and subtraction, you could use any operand. But if you don't want intermediate results to go insanely high, you might want to restrict them. For multiplication, the operands are restricted to the divisors of the result. For addition, the operands must obviously be smaller than the result.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • Okay that sounds exactly like what I am attempting to do. One problem I believe may arise is how do I prevent going to zero while interating, thus not being able to apply further operations. Or should I be worried about that? - I suppose I could always add/subtract if I hit zero. Your thoughts? – PeterLNewton Oct 02 '16 at 18:32
  • So as you suggested, I do want to stay in a certain range. Suppose I could use probability to isolate values that may result in out of range numbers. Would you mind elaborating on this? Control is important, don't want uncontrollable randomness. – PeterLNewton Oct 02 '16 at 18:48
  • You do not need probability. For every operation, there will be a number of operands that produce numbers in a given interval. Just pick one of these. Zero won't be a problem. It will just have no applicable operands for + and /. You could just specify your interval to exclude zero. – Nico Schertler Oct 02 '16 at 19:24
  • I understand what you're saying. Okay thanks for your time. I'll post back here after giving this a try. – PeterLNewton Oct 02 '16 at 19:35