Introduction
This is one of my favorite math puzzles.
Given a digit (say 3) and the number of times to use that digit (say 5), generate 10 expressions which result to 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 using just +, −, ×, ÷, ^ and √ (root) (brackets are allowed to group operations).
For example:
(3^3 + 3)/(3 + 3) = (33 - 3)/(3 + 3) = 3 + 3/3 + 3/3 = 5
Note that all of the above use five 3’s and the mathematical operations and result to 5. You can also use a 3 before √ to denote a cube root. Same goes for using 4 before √ to denote a fourth root.
Also note that two 3’s can be used to form 33, or three 3’s can be used to form 333 and so on.
Challenge
- You will be given two numbers (both ranging from 1 to 5) as a function argument, STDIN or command line argument.
- The first number denotes which digit to use and the second number denotes the number of times that digit is to be used in the expression.
- Your program should output an array of size 10 (or 10 space-separated numbers) where each element denotes whether a mathematical expression (using just the allowed operators) resulting into the
(index + 1)
number is possible or not using a truthy/falsy value.
For example, if the input is
1 3
Then the output should be
[1, 1, 1, 0, 0, 0, 0, 0, 0, 1]
because only 1, 2, 3 and 10 can be expressed using three 1’s.
Score
- This is a code-golf so the minimum code length in bytes wins.
Bonus
Print-em-all [−50]
Subtract 50 from your score if the output array elements are equal to the total number of plausible combinations to get the (index + 1)
value instead of truthy or falsy values.
For example, if there are only 3 possible combinations of five 3’s which result to 5, then the output array’s 4th entry should be 3.
Extreme Maths [−100]
Subtract 100 from your score if the output array elements contain at least one of the actual expressions which result to the (index + 1)
value.
For example, if using five 3’s, the output array’s 4th entry can be either (3^3 + 3)/(3 + 3)
, (33 - 3)/(3 + 3)
or 3 + 3/3 + 3/3
Overkilled [−200]
Subtract 200 from your score if the output array elements contain all possible combinations (separated by |
). This bonus is added on top of the Extreme Maths bonus, so you get −300 in total.
For example, if using five 3’s, the output array’s 4th element should be (3^3 + 3)/(3 + 3)|(33 - 3)/(3 + 3)|3 + 3/3 + 3/3
Note: Any two expressions to achieve the same result should be logically different with a different approach in both of them.
For instance, to get 5 using five 3’s, 3 + 3/3 + 3/3
is same as 3/3 + 3 + 3/3
or 3/3 + 3/3 + 3
because the same approach is taken for each of them. (3^3 + 3)/(3 + 3)
and (33 - 3)/(3 + 3)
differ, as the 30 in the numerator is achieved via different approaches.
UPDATE : After going through all answers, it was found that all answers had imperfections due to edge cases of unary -
and √. Thus, missing those edge cases was considered okay as far as completeness of answers is involved.
This is a tough question, but a rather interesting one.
Happy golfing!
1
s? \$\endgroup\$