Create an arbitrary number from numbers and simple operations

How to calculate an arbitrary integer using the smallest elementary arithmetic operations (add, sub, mult, div) for integers with one digit as optimally as possible?

Numbers 0-9 have 0 values. Other numbers must be formed using elementary operations.

Examples: 25 is created with 5 * 5

123 can be created in many ways, but the most optimal is 5 * 5 * 5-2.

At first I wanted to solve it using dynamic programming, but I could not overcome the obstacles to introducing multiplication, and I do not think that this is practical for large numbers. But if it is, please tell me how to do it.

If someone can direct me to the right problem like this, I would be grateful.

+6
source share
2 answers

Unfortunately, the answer you are looking for is not as simple as it might be for other questions about SO. The consequence of this was that you were voted several times. Maybe I couldn’t pull the trigger so fast, but that’s the right question.

I would suggest that you define a set of numbers that can be achieved with only one arithmetic operation (all integers from -9 to 18 and numbers appearing in the multiplication table), with two arithmetic operations ... with three arithmetic operations, and t .d., so membership in the solution established for a given number of arithmetic operations can determine the lower bound on the size of the operator for a given target integer x:

{x ε I} c A ( N );

The singleton "x" element of integers is a subset of A ( N ), where A ( N >) is defined as the set of possible solutions after performing N possible arithmetic operations. (The upper bound ( N ), defined as the number of arithmetic operations in "1 + 1 + 1 ... + 1 + 1" for positive numbers and "0-1-1 ... -1 -1" for negative numbers)

Then the set of optimal solutions will be determined as follows:

A (infimum ( N )), where infimum ( N ) minimizes the number of arithmetic operations.

Editorial: Last night I made another pencil example, and earlier I suggested non-prime numbers as members of A (1), which would be an exception in any non-empty number with a prime factor> = 11. The smallest of them is 22, as indicated below.

If we start with A (0), defined as a closed set of integers [0,9] Then A (n + 1) = u ( A (n), A (0)), u is a set of arithmetic functions.

Since division cannot be part of an optimal solution due to the existence of the least common multiple for irrational results, then u '(x, y) = {x + y, x - y, x * y}; smallest restrictive set containing x and y, which is an integer.

The same elimination is not performed for subtraction, because we need a neighborhood above and below the product.

We can also eliminate zero from all optimal solutions, leaving A '(0) = [1,9]. because we note that singleton {0} ε A (0), and since, by the definition of zero, <

0 * A (n) = 0 and
0 + A (n) = A (n)

The optimal solution for zero is always "0", and since any occurrence of "+0+" or zeros in the product space cancels the entire product or the step of adding zero, the solution is not optimal and therefore can be excluded from the search.

So, one way in the pseudocode to find the whole solution established for A (n), n ε N (naturals), neglecting the order of operations, would be:

**A**(n) = **A**(n-1); // n*1 is still included, // and n+0 is by proxy included for posterity for x ϵ **A**(n-1) for y ϵ **A'**(0) for u ϵ **u'** A(n) = Union( A(n), u(x,y) ); next u next y next x 

Remember, I cannot determine if the result is sorted or not. And we neglected the order of operations, so this is not a complete, complete algorithm for the solution you are looking for, and then I nod, so I will edit it again and describe the order of operations.

EDITED: Operational Description

Firstly, thanks for making this decision, and as I said last night, I would describe the order of operations.

Since we are dealing with multiplication specifically, then we must first find the products inside the operator, so that we will have a set of P , which will be a set of terms and evaluated products, within an acceptable statement; we will not represent P with a string.

So, first we need to cross the string and look for multiplication operations so that we can create an array P of integers, the summation of which is the solution for the operator. In pseudocode, it might look like this:

 array **P** = empty // the upper limit for the bounds of **P** // is number of operators - number of multiplications + 1 first **P** = first **d** // we can initialize the first value of **P** for j ϵ N, j <= n // j is a natural number and 'n' is the number of operators given u(j) ϵ **u** // **u** is the set of operations given d(j) ϵ **d** // **d** is the set of digits of length n + 1 if u(j) is not multiplication { **P** = Union( **P**, d(j+1) ); // append d(j+1) to the end of **P** if u(j) is subtraction negate last **P** // then last **P** is negative } else //---> u(j) is multiplication { last **P** *= d(j+1) } next j solution = sum **P**; solution ϵ I. 

We now have a routine to evaluate the statement in relation to the order of operations, which we can call eval . Returning to the previous structure of the loop, the optimization of the recursive property breaks when we follow the order of operations.

 ("2+4*3" = 14 not 18) 

So, we are stuck in a routine that has to search through the set of possible operators for operations "n". Then we can narrow the set of statements by excluding addition and multiplication by zero and further, excluding multiplications by one, which, it seems, you also did. I assume that the cryptic one you indicated checks not only that the previous operation was u ε {+, -} before adding the number “1”, but also that the previous number was not “1” before adding the operation u ε {*}.

As soon as we can search the optimized set of statements of a certain length for the target integer, we can iterate the length property of the operators until the target integer is an evaluated solution, then we can start saving the instructions of minimized length by evaluating the target integer into an array of strings and stopping after iteration through other operators of a given size.

This will be an exhaustive search. Even if we checked the target integer for divisibility by the production space of the digits, what happens when we cannot break it down into a prime numeric number? Can you suggest optimization of number theory, given the set of integers that are represented by the product space with single digits with an indefinite length, having a mapping that indicates the optimal solution for the space and the number of multiplications in the product space? If not, then we are stuck with an exhaustive search, for now.

+3
source

After reading @JustKevin's answer and rethinking the problem, I found a heuristic that is probably good enough for me.

It is based on dynamic software and finds the length of the shortest sequence of operations to achieve a number, but can be easily modified to create this sequence.

(if someone can fix the formatting, please, as I am writing from the phone, and this is acting weirdly on me)

So:

Array M returns infinity for all unset arguments, and c(i,x) describes the cost of converting i to x. N is the number of the target.

 M[0..9]=0 c(i,x) = min( if x==i then 0, if x>i then M[xi] + 1, //addition if x<i then M[-x+i] + 1, //subtraction min( for y=2,9 yield c(i*y,x) + 1 end) //multiplication ) for x=10,N do for i=1,x-1 do M[x]= min(M[x], M[i]+c(i, x) end end 

The answer is in M[N] .

If you find a possible error, let me know.

0
source

Source: https://habr.com/ru/post/973877/


All Articles