11
0
In this variant of the Four fours puzzle your should use up to x
x's
(and no other number) and a defined set of operations to reach every number from 0 to 100. If x = 4
then you can use up to four 4s
and this question becomes the classic four fours puzzle (except you can use up to four 4s rather than having to use exactly four of them). We assume 1 < x <= 9
.
In this version, only the following operators are allowed:
- Addition (
+
), Subtraction (-
), Multiplication (*
), Division (/
). Note this is real division, so that5/2 = 2.5
. - Exponentiation (e.g. 4^4) as this would involve no extra symbols if written normally by hand.
- You can make new integers by concatenating
xs
. E.g. you can make the integers4, 44, 444, 4444
.
You may also use parentheses to group numbers simply in order to control the order of evaluation of the operators. You can't for example combine parentheses with concatenation as in (4/4)(4/4) = (1)(1) = 11
.
No other symbols may be used and standard order of operations applies.
Your program should generate, given an x
in the defined range and an n
between 0
and 100
inclusive, a correct solution for that input if it exists. Otherwise your code must output something to indicate no such solution exists.
You must be able to run your submission to completion on your machine for any input values of x
and n
in the allowed range. This is code golf, so shortest solution wins.
This old related question uses more operators (and only 4s) and hence all numbers from 0 to 100 are solvable which won't be true for this challenge.
Input and output
Your code takes two integers x
and n
as input and should output a solution (or an indication there is no solution) in any human readable format you find convenient. Input 4 6
would mean "Using up to four 4s, make the number 6" for example. So if the input is 4 6
the output could be (4+4)/4+4
.
Parentheses allowed: yes or no? – Digital Trauma – 2018-05-15T20:48:03.160
@DigitalTrauma Thank you I should have said. Yes. – Anush – 2018-05-15T20:49:39.657
1Helpful Reference – Engineer Toast – 2018-05-15T20:51:07.360
2Can parens be combined with concatenation? e.g. `(4/4)(4/4) = (1)(1) = 11 ? – Digital Trauma – 2018-05-15T20:52:30.863
@DigitalTrauma Oh wow. No! I will clarify. – Anush – 2018-05-15T20:53:35.057
1Adding parentheses (and disallowing parentheses + concatenation) does make this significantly harder – Draconis – 2018-05-15T21:16:11.013
Can we use the negation operator?
(-4)
– mbomb007 – 2018-05-16T13:38:45.177@mbomb007 Yes you can. – Anush – 2018-05-16T13:39:36.307
1I guess a more important question is this: is it be necessary to use the negation operator to reach a result for a possible input? – mbomb007 – 2018-05-16T14:49:54.313
@mbomb007 No . – user202729 – 2018-05-16T15:36:54.753
2
Adding the exponentiation operator and an outer loop over the number of times the digit is used don't IMO add anything non-trivial over https://codegolf.stackexchange.com/q/82884/194
– Peter Taylor – 2018-05-16T16:11:54.513Personally, I think it'd be better to output every possible integer in the range, or given an integer output a Boolean whether it's possible to arrive at. Outputting an expression has been done before. – mbomb007 – 2018-05-16T18:48:11.040
1@PeterTaylor Do you think the use of parentheses and the requirement that it terminates in reasonable time for all input values makes a difference? Certainly no one has offered a 23 byte solution to this question. – Anush – 2018-05-16T20:55:01.670
I don't think the parentheses make much of a difference, and the time limit is really quite lax (there aren't so many distinct expressions when all the values are the same). OTOH I'm not the only person who can cast a reopen vote: you're welcome to make your case in the chat room and get some third, fourth, etc. opinions. – Peter Taylor – 2018-05-16T22:19:53.233
@PeterTaylor I just tried the ones I could test from the linked question with
[4,4,4,4] 4
. It seems they give no solution. That is they can neither find4
(because they have to use all the numbers) nor(4*(4-4)+4)
(because they can't use parentheses). – Anush – 2018-05-17T12:18:27.180See "outer loop" in my first comment. – Peter Taylor – 2018-05-17T13:52:39.390
2@PeterTaylor The parentheses seem to make quite a lot of difference. I would vote to reopen if I could. – felipa – 2018-05-17T13:58:28.497