28
\$\begingroup\$

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 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:

Dyalog APL:

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 and sy pop an element from the stack and save it on the registers x and y, respectively.

  • lx and ly load an element from registers x and y 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 because dc has no logarithm function.

  • [...]sa stores a macro in register a. 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
\$\endgroup\$
  • \$\begingroup\$ I assume conversion from integer to string is invalid? \$\endgroup\$ – Anthony Pham Apr 5 '17 at 23:56
  • 2
    \$\begingroup\$ I think we've had a challenge much like this but don't know what terms would find the dupe. \$\endgroup\$ – xnor Apr 5 '17 at 23:58
  • 2
    \$\begingroup\$ @AnthonyPham "You're not allowed to use strings or any kind of concatenation anywhere in your code." \$\endgroup\$ – ASCII-only Apr 6 '17 at 2:01
  • 1
    \$\begingroup\$ Can we take a pair of integers as input? \$\endgroup\$ – Conor O'Brien Apr 6 '17 at 2:58
  • 1
    \$\begingroup\$ Can I make a full program instead of a function? \$\endgroup\$ – Erik the Outgolfer Apr 6 '17 at 14:08

24 Answers 24

10
\$\begingroup\$

JavaScript (ES7), 63 61 59 bytes

Saved 4 bytes thanks to Neil.

(a,b)=>[a*b,a+b,a-b].reduce((p,c)=>p*10**-~Math.log10(c)+c)

<input id=a type=number oninput="c.innerText=((a,b)=>[a*b,a+b,a-b].reduce((p,c)=>p*10**-~Math.log10(c)+c))(+a.value,+b.value)">
<input id=b type=number oninput="c.innerText=((a,b)=>[a*b,a+b,a-b].reduce((p,c)=>p*10**-!Math.log10(c)+c))(+a.value,+b.value)">
<p id=c>

\$\endgroup\$
  • \$\begingroup\$ Save a few bytes using 10**-~Math.log10(c). (But +1 for using reduce, of course.) \$\endgroup\$ – Neil Apr 6 '17 at 9:11
  • \$\begingroup\$ "ES7" Oh, for the love of coding... They're making another one? \$\endgroup\$ – Feathercrown Apr 7 '17 at 14:12
  • \$\begingroup\$ @Feathercrown Yeah, but is it really worse than hearing "Java 9"? Plus it has useful things like async/await and the exponentiation operator ** \$\endgroup\$ – ASCII-only Apr 7 '17 at 14:13
  • \$\begingroup\$ @ASCII-only ** is really useful, I agree. That should've been in ES6. \$\endgroup\$ – Feathercrown Apr 7 '17 at 14:17
8
\$\begingroup\$

C, 79 75 bytes

Thanks to @G B for saving 4 bytes!

#define X for(c=1;(c*=10)<=a
c,d;f(a,b){X+b;);d=c*a*b+a+b;X-b;);a=d*c+a-b;}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Save some bytes by using a define instead of a function: tio.run/nexus/… \$\endgroup\$ – G B Apr 6 '17 at 10:43
6
\$\begingroup\$

Bash, 66

  • 2 bytes saved thanks to @chepner.
f()(s=$[$1+$2]
d=$[$1-$2]
echo $[($1*$2*10**${#s}+s)*10**${#d}+d])

Try it online.

\$\endgroup\$
  • \$\begingroup\$ It is possible to make this almost two times shorter if you place your variables(s, d and the other one for multiplication that you didn't define) next to each other and evaluate that as an arithmetic expression. \$\endgroup\$ – Maxim Mikhaylov Apr 6 '17 at 2:22
  • 3
    \$\begingroup\$ @MaxLawnboy Yes, though that sounds a lot like the banned string concatenation to me. \$\endgroup\$ – Digital Trauma Apr 6 '17 at 2:28
  • 1
    \$\begingroup\$ Identifier names inside $[...] are subject to parameter expansion without the explicit $ (e.g., d instead of $d), saving two characters. \$\endgroup\$ – chepner Apr 7 '17 at 1:12
  • \$\begingroup\$ @chepner yep - thanks - I'd missed those. \$\endgroup\$ – Digital Trauma Apr 7 '17 at 1:20
  • \$\begingroup\$ Found another two; use ((s=$1+$2,d=$1-$2)) to intialize the two variables. \$\endgroup\$ – chepner Apr 7 '17 at 1:23
5
\$\begingroup\$

EXCEL, 61 Bytes

=A1-B1+(A1+B1)*10^LEN(A1-B1)+A1*B1*10^(LEN(A1-B1)+LEN(A1+B1))

Excel, 18 Bytes not valid

=A1*B1&A1+B1&A1-B1
\$\endgroup\$
5
\$\begingroup\$

Stacked, 36 bytes

,@A$(*+-){!A...n!}"!{%y#'10\^x*y+}#\

Try it online!

Previously: ,@A$(-+*){!A...n!}"!:inits$#'"!$summap:pop@.10\^1\,\*sum

I'm going to try to squeeze out a byte or two before writing an explanation. (#' = size of, and " is "do on each", no strings attached here.)

Noncompeting at 26 bytes: $(*+-)#!!:{%y#'10\^x*y+}#\.

\$\endgroup\$
5
\$\begingroup\$

TI-Basic, 34 33 bytes

Prompt A,B
A-B+(A+B)10^(1+int(log(A-B
Ans+AB10^(1+int(log(Ans
\$\endgroup\$
  • \$\begingroup\$ I think Prompt A,B should also work \$\endgroup\$ – Conor O'Brien Apr 6 '17 at 2:56
  • \$\begingroup\$ @ConorO'Brien You're right! \$\endgroup\$ – pizzapants184 Apr 6 '17 at 4:46
4
\$\begingroup\$

GNU dc, 36

Defines a macro m that takes the top two members of the stack, applies the macro and leaves the result on the stack (as per the example in the question):

[sadsbla-dZAr^lalb+*+dZAr^lalb**+]sm

Try it online.

\$\endgroup\$
3
\$\begingroup\$

Perl 6,  81 61  58 bytes

->\x,\y{($/=($/=x- y)+(x+y)*({10**$++}...*>$/).tail)+x*y*({10**$++}...*>$/).tail}

Try it

->\x,\y{(x*y,x+y,x- y).reduce:{$^a*10**Int(1+log10($^b))+$b}}

Try it

->\x,\y{[[&({$^a*10**Int(1+$^b.log10)+$b})]] x*y,x+y,x- y}

Try it

\$\endgroup\$
  • \$\begingroup\$ Not knowing Perl 6, I was slightly surprised to discover that x-y is a valid identifier. \$\endgroup\$ – Neil Apr 6 '17 at 9:18
3
\$\begingroup\$

Jelly, 27 bytes

+,ạ,.1Fl⁵Ḟ‘€Ṛ+\⁵*
ạ,+,×Fæ.ç

Defines a dyadic link / function, callable with ç. Takes two integers as input and returns an integer. It has the added bonus of being able to take x<y or x>y by using absolute difference.

Try it online!

Explanation:

+,ạ,.1Fl⁵Ḟ‘€Ṛ+\⁵* -- Create link which computes what order of magnitude
                        to multiply the difference, sum, and product by
ạ,+,×Fæ.ç         -- Main link, applies this using dot product

Details:

+,ạ,.1Fl⁵Ḟ‘€Ṛ+\⁵* -- Create dyadic like which does the following:
       l⁵Ḟ‘       -- Create operation which computes number of digits
                       (log base 10 (⁵ is the literal 10), floored, incremented)
           €      -- Apply this to each element in
+,ạ,.1F           -- ... the list [sum,difference,.1]
            R     -- Reverse the list
             +\   -- Add up first n elements to get list.
               ⁵* -- Raise 10 (⁵ is literal 10) to the power of each element

ạ,+,×Fæ.ç         -- Main link, applies above link
ạ,+,×F            -- The list [difference, sum, product]
      æ.          -- Dot product (multiply corresponding elements) with
        ç         -- The above link.
\$\endgroup\$
2
\$\begingroup\$

PHP, 79 75 bytes

two versions:

[,$a,$b]=$argv;echo(10**strlen($s=$a+$b)*$a*$b+$s)*10**strlen($d=$a-$b)+$d;
[,$a,$b]=$argv;echo(10**strlen($a+$b)*$a*$b+$a+$b)*10**strlen($a-$b)+$a-$b;

takes input from command line arguments; run with -r.

I guess strlen qualifies as "function that returns the number of digits",
although it uses the number as a string. Let me know if not.

\$\endgroup\$
  • \$\begingroup\$ 'You're not allowed to use strings or any kind of concatenation anywhere in your code.', so I don't think strlen is valid. \$\endgroup\$ – numbermaniac Apr 20 '17 at 7:45
  • \$\begingroup\$ @numbermaniac Let the OP decide. Imo the restriction was to force the solutions to create one result instead of just printing three results after each other. Everything beyond that is nitpicking. \$\endgroup\$ – Titus Apr 20 '17 at 13:17
2
\$\begingroup\$

C (gcc), 70 bytes

#define _ for(c=1;a+b>=(c*=10););d=c*d+a-(b=-b);
c,d;f(a,b){d=a*b;_ _}

Try it online!

based on Steadybox answer, putting everything in a macro to golf it a little more.

(Note: assigning the result to d instead of a works, unexpectedly. I had a look at the generated assembly code and it seems to be ok.)

\$\endgroup\$
2
\$\begingroup\$

Haskell, 54 bytes

a%0=a
a%b=10*a%div b 10+mod b 10
a#b=(a*b)%(a+b)%(a-b)

The puzzle is implemented via an infix function #, e.g. 8#2 = 16106. The other function, %, defines base-10 concatenation (assuming the RHS is greater than 0).

\$\endgroup\$
2
\$\begingroup\$

Dyalog APL, 31 bytes

{a⊥⍨10*1+⌊10⍟a←(⍺×⍵)(⍺+⍵)(⍺-⍵)}

based on the sample APL code from the problem statement

\$\endgroup\$
1
\$\begingroup\$

PHP, 87 Bytes

[,$a,$b]=$argv;echo($s=$a-$b)+($t=$a+$b)*10**($l=strlen($s))+$a*$b*10**($l+strlen($t));

and a not valid solution for 37 Bytes

[,$a,$b]=$argv;echo$a*$b,$a+$b,$a-$b;
\$\endgroup\$
1
\$\begingroup\$

Ruby, 61 bytes

->a,b{[a*b,a+b,a-b].reduce{|x,y|z=y;x*=10while(z>z/=10);x+y}}

Which suspiciously looks a lot like this Javascript answer, but without using a logarithm.

\$\endgroup\$
1
\$\begingroup\$

Python, 92 91 Chars

def g(x,y):
    l=lambda x,i=0:l(x/10,i+1)if x else 10**i
    a=x-y
    a+=(x+y)*l(a)
    return x*y*l(a)+a

Thanks to Wizards suggestion;)

\$\endgroup\$
  • \$\begingroup\$ Welcome to the site! You don't need the space between ) and if. \$\endgroup\$ – Wheat Wizard Apr 6 '17 at 17:40
1
\$\begingroup\$

R (3.3.1), 104 bytes

function(x,y)Reduce(function(p,q)p*10^(floor(log10(q)+1))+q,lapply(c(`*`,`+`,`-`),function(z)z(x,y)),0)

returns an anonymous function.

This is my first golfing attempt, so any feedback is appreciated.

\$\endgroup\$
  • 1
    \$\begingroup\$ I'd say to try to avoid defining functions via the reserved word 'function' if at all possible, it uses up a lot of bytes. Just do the computation. \$\endgroup\$ – user11599 Apr 21 '17 at 5:18
0
\$\begingroup\$

REXX, 70 bytes

f:arg a b
c=a-b
c=(a+b)*10**length(c)+c
c=a*b*10**length(c)+c
return c

Of course, the native way would be much shorter:

f:arg a b
return a*b||a+b||a-b
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 88 Bytes

param($x,$y)$l=2;if(($a=$x+$y)-gt9){$l++};($x*$y)*[math]::Pow(10,$l)+$a*10+$x-$y

PowerShell does not have a to the power operator which does not help. Also can't count the length of an integer unless you count it as a string, which we can not do, so I check to see if it is -gt 9 to get know the length. Likely could be more terse but I have to get back to work.

\$\endgroup\$
0
\$\begingroup\$

Python 2.7, 109 96 bytes

import math
a=lambda n:10**int(math.log10(10*n))
b,c=input()
d=b-c+(b+c)*a(b-c)
print d+b*c*a(d)

Corrected after following rules of contest. Credits to mbomb007 for bringing down the code from 109 bytes to 96 bytes

\$\endgroup\$
  • 1
    \$\begingroup\$ From the rules of this challenge -- •You're not allowed to use strings or any kind of concatenation anywhere in your code. \$\endgroup\$ – AdmBorkBork Apr 6 '17 at 12:56
  • \$\begingroup\$ You can save some bytes by making a a lambda. a=lambda n:10**int(.... You can also do b,c=input(), giving your two inputs separated by a comma. \$\endgroup\$ – mbomb007 Apr 6 '17 at 14:34
  • \$\begingroup\$ @mbomb007 b,c=input() gives TypeError: 'int' object is not iterable. I've tried it. And the lambda function won't save bytes because I'm calling the function twice in the code. tried that too. :( \$\endgroup\$ – Koishore Roy Apr 6 '17 at 14:53
  • \$\begingroup\$ @KoishoreRoy I don't think you get what I mean. 96 bytes \$\endgroup\$ – mbomb007 Apr 6 '17 at 15:20
0
\$\begingroup\$

J, 25 bytes

X=.10#.[:;10#.inv&.>*;+;-
  1. *;+;- Box the results of each operation.
  2. 10#.inv&.> Convert each result into an array of base-10 digits. (inv is ^:_1)
  3. [:; Unbox and join the arrays.
  4. 10#. Convert array of base-10 digits into an integer.
  5. X=. define the above as the operator X.

Results:

   8 X 2
16106
   5 X 4
2091
   9 X 6
54153
\$\endgroup\$
  • \$\begingroup\$ You don't need X=. \$\endgroup\$ – Cyoce Apr 7 '17 at 18:28
  • \$\begingroup\$ @Cyoce - the sample APL code in the challenge defines an operator. I'm pretty sure we're supposed to define a reusable operator for this challenge. \$\endgroup\$ – Dane Apr 7 '17 at 18:54
  • \$\begingroup\$ "3. [:; Unbox and join the arrays." - "You're not allowed to use strings or any kind of concatenation anywhere in your code." \$\endgroup\$ – ngn Apr 13 '17 at 7:15
  • \$\begingroup\$ @ngn - Please expand on your comment. At no point are strings used. \$\endgroup\$ – Dane Apr 13 '17 at 16:57
  • \$\begingroup\$ I just wanted to point out that "join" ("link"?) might constitute a "kind of concatenation", though I'm not very familiar with J, and I'm not sure how to interpret the problem statement in this case. My own solution raises similar questions - I use stranding (juxtaposition of nouns in APL forms a vector) which might be the same as J's "link" but without a glyph to represent it. \$\endgroup\$ – ngn Apr 13 '17 at 19:36
0
\$\begingroup\$

Mathematica, 67 bytes

c=Ceiling;l=Log10;#-#2+(#+#2)10^(c@l[#-#2]/. 0->1)+10^c@l[2#]10#2#&

Takes x-y, then takes the log10 of x-y, rounds it up, calculates 10 to the power of that and then multiplies it by x+y. But we also need to consider log10(x-y) being 0, so we replace 0 with 1. Then we take the log10 of 2x, rounded up, plus 1, and find 10 to the power of that. Multiply that by xy, and add that on.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 23 22 16 bytes

-Dg°¹²+*Dg°¹²**O

Try it online!

We could have saved a few bytes if we'd been allowed to use strings in the program (but not in calculations) by looping over a string containing the operations "-+*", as the code performed for each operation is the same.
Of course, if we'd been allowed to use concatenation we'd saved a lot more.

\$\endgroup\$
0
\$\begingroup\$

R, 64 bytes

x=scan();(b=(a=diff(-x))+10^nchar(a)*sum(x))+10^nchar(b)*prod(x)

Usage:

> x=scan();(b=(a=diff(-x))+10^nchar(a)*sum(x))+10^nchar(b)*prod(x)
1: 8 2
3: 
Read 2 items
[1] 16106
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.