28
4
Challenge:
There's a silly puzzle circulating on social networks that reads:
8 + 2 = 16106
5 + 4 = 2091
9 + 6 = ?
Implement a function or operator that, when given two positive integer numbers x
and y
such that x > y > 0
, yields the correct answer as an integer, where the answer's digits are the digits of x * y
followed by the digits of x + y
followed by the digits of x - y
. Very simple.
Rules:
- Standard loopholes are disallowed.
- This is code-golf so shortest code in bytes wins.
- Input data validation is not required. This program may crash or return garbage when given invalid input.
- You're allowed to use numeric functions and operators (including integer and floating point, math library functions, and other functions that accept and return numbers).
- You're allowed to use a function that returns the number of digits of a number, if applicable.
- You're not allowed to use strings or any kind of concatenation anywhere in your code.
- The result may be returned or pushed to the stack, whichever applies in the language. The result must be an integer number, not a string.
Sample code:
The following code creates a dyadic operator named X
.
X←{(⍺-⍵)+((⍺+⍵)×10*1+⌊10⍟⍺-⍵)+⍺×⍵×10*(2+⌊10⍟⍺+⍵)+⌊10⍟⍺-⍵}
Explanation:
In APL, you evaluate from right to left.
⍺ and ⍵
are the left and right operand, respectively⌊10⍟⍺-⍵
reads:floor of log10(⍺-⍵)
. First performs substraction then logarithm then floor. From right to left. log10 is done in order to count the digits of⍺-⍵
(you must sum 1 afterwards).⍺×⍵×10*(...)
reads:10 to the (...)th power, multiplied by ⍵, multiplied by ⍺
Hence,
⍺×⍵×10*(2+⌊10⍟⍺+⍵)+⌊10⍟⍺-⍵
is the product, shifted to the left by the sum of the number of digits of the sum and the difference. Multiplying by a power of 10 will shift an integer to the left.((⍺+⍵)×10*1+⌊10⍟⍺-⍵)
is the sum, shifted to the left by the number of digits of the difference.(⍺-⍵)
is the difference. No shifting is necessary here.X←{...}
is how you define an operator in APL.
Examples:
8 X 2
16106
5 X 4
2091
9 X 6
54153
GNU dc:
The following code creates a macro named a
:
[sysx10lxly-dseZdsclxly+dsd+Z1+^lxly**10lc^ld*+le+]sa
Explanation:
sx
andsy
pop an element from the stack and save it on the registersx
andy
, respectively.lx
andly
load an element from registersx
andy
respectively and push it to the stack.d
duplicates the last element in the stack.^
computes the power of two numbers.Z
pops a number and returns its number of digits. This is done becausedc
has no logarithm function.[...]sa
stores a macro in registera
.la
loads it.x
executes the macro at the top of the stack.
Examples:
8 2 laxn
16106
5 4 laxn
2091
9 6 laxn
54153
I assume conversion from integer to string is invalid? – Anthony Pham – 2017-04-05T23:56:47.807
2I think we've had a challenge much like this but don't know what terms would find the dupe. – xnor – 2017-04-05T23:58:51.577
2@AnthonyPham "You're not allowed to use strings or any kind of concatenation anywhere in your code." – ASCII-only – 2017-04-06T02:01:32.173
1Can we take a pair of integers as input? – Conor O'Brien – 2017-04-06T02:58:21.367
1Can I make a full program instead of a function? – Erik the Outgolfer – 2017-04-06T14:08:04.987
@Locoluis - Does appending integer arrays violate your rule against "any kind of concatenation"? What I'm effectively doing is taking one integer array
2 4 8
and appending another3 5
to create2 4 8 3 5
in my answer. – Dane – 2017-04-13T21:24:19.727'No strings or concatenation' is a non-observable rule, as such, it's highly discouraged in a challenge. – ATaco – 2017-04-20T04:37:28.557
Does
strlen
qualify as "a function that returns the number of digits of a number"? There´s an implicit typecast which I cannot avoid; and I doubt that there is any language who has an explicitcount_digits
function. Also, printing a result implies a typecast to string ... So ifstrlen
should be banned, printing is at least questionable. – Titus – 2017-04-20T13:11:13.987