I used to play a game while I was in the car, it consisted in taking the
n-1 first numbers of another car plate and use operators and roots on them to get the
nth number of the plate.
I could use the following operators and roots :
- addition (ex: 1 + 1 = 2)
- subtraction (ex: 2 - 4 = -2)
- division (ex: 10 / 2 = 5) (Note that the division is only allowed if it returns an integer)
- multiplication (ex: 7 * 7 = 49)
- square root (ex : sqrt(36) = 6) (Note : must return an integer to be used)
- cubic root (ex : cbrt(8) = 2) (Note : must return an integer to be used)
- multiplication by -1 (ex : 8 * -1 = -8)
- power 2 (ex : 2² = 4)
- power 3 (ex : 2^3 = 8)
- leave the integer as it was (ex : 3 = 3)
The operations must be done in order! Which means you have to do the operations between the first and the second integer before an operation between the second and the third.
Example : you can do
(3 + 9) * 4 but not
3 + 9*4 if you have
[3,9,4,x,y,z,...] as input
Moreover, you can use a unary operator on an integer before doing an operation.
Example : you can do
3² - 7^3
But powers and roots are only allowed on one integer and not on the results of previous operations.
Example : you're not allowed to do
Note that multiplication and division are done that way : it's the applications of the previous operations that's multiplied or divided. For instance :
[x,y,z,t,h] => (x+y)/z + t
[x,y,z,t,h] => (x-y+z) * t
Your code will take as input an array of integers of size
n-1 first integers will be the one used to obtain the
nth number of the array.
You may use all the operators,roots and powers allowed by the rules above. Remember the integers must stay in the same orders to do the operations!
Your code will output a falsey value or an error if the
nth number of the array cannot be obtained with the
n-1first numbers of the array. On the other hand, if you can obtain the
nth number you have to display the operations that led to it. Your code needs to output only one way of obtaining the
(Note that I write square root sqrt and cube root cbrt but you can write it the way you want : x**(1/3) for instance)
 => error or 1
[2,8] => 2^3
[3,7,46] => 3*(-1) + 7²
[1,2,3,4] => 1*(-1) + 2 + 3
[0,0,0,0,0,0,0,8] => error
[7,4,3,8,16] => 7 + 4² - 3² + cbrt(8)
[28,4,8,7,5,1] => (28 - 4² - cbrt(8) - 7 ) / 5
This is code-golf, thus the shortest answer in bytes wins!
3*(-1) + 7²is allowed? Should it be
[3*(-1) + 7]²? \$\endgroup\$
[3*(-1) + 7]²as you described would be suited for a division or a multiplication but not a unary operator \$\endgroup\$