Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Goal

Your goal is to multiply two numbers using only a very limited set of arithmetic operations and variable assignment.

  1. Addition x,y -> x+y
  2. Reciprocal x -> 1/x (not division x,y -> x/y)
  3. Negation x -> -x (not subtraction x,y -> x-y, though you can do it as two operations x + (-y))
  4. The constant 1 (no other constants allowed, except as produced by operations from 1)
  5. Variable assignment [variable] = [expression]

Scoring: The values start in variables a and b. Your goal is to save their product a*b into the variable c using as few operations as possible. Each operation and assignment +, -, /, = costs a point (equivalently, each use of (1), (2), (3), or (4)). Constants 1 are free. The fewest-point solution wins. Tiebreak is earliest post.

Allowance: Your expression has to be arithmetically correct for "random" reals a and b. It can fail on a measure-zero subset of R2, i.e. a set that has no area if plotted in the a-b Cartesian plane. (This is likely to be needed due to reciprocals of expressions that might be 0 like 1/a.)

Grammar:

This is an . No other operations may be used. In particular, this means no functions, conditionals, loops, or non-numerical data types. Here's a grammar for the allowed operations (possibilities are separated by |). A program is a sequence of <statement>s, where a <statement> is given as following.

<statement>: <variable> = <expr>
<variable>: a | b | c | [string of letters of your choice]
<expr>: <arith_expr> | <variable> | <constant>
<arith_expr>: <addition_expr> | <reciprocal_expr> | <negation_expr> 
<addition_expr>: <expr> + <expr>
<reciprocal_expr>: 1/(<expr>)
<negation_expr>: -<expr>
<constant>: 1

You don't actually have to post code in this exact grammar, as long as it's clear what you're doing and your operation count is right. For example, you can write a-b for a+(-b) and count it as two operations, or define macros for brevity.

(There was a previous question Multiply without Multiply, but it allowed a much looser set of operations.)

share|improve this question
3  
Is this even possible? –  Ypnypn Sep 1 at 20:19
1  
This feels like a challenge where an optimal solution is likely to be found (once any solution has been found). So what's the tie breaker in that case? –  Martin Büttner Sep 1 at 20:27
1  
@MartinBüttner Tiebreak is earliest posting in that case. I think there's a good amount of room for optimizations, so I don't think it will just be a race to find one that works and write it cleanly. At least, that's what I found in trying it; maybe someone will find a clearly minimal solution. –  xnor Sep 1 at 20:30
1  
Ok since not everyone thought my anwer was as funny as I did, I deleted it and comment here: The rule about the measure zero set is not very wisely chosen since rational numbers are a measure zero set regarding the lebesgue measure, I'd suggest using a certain percentage instead. (Or another kind) But I totally like the idea of this challenge! –  flawr Sep 1 at 21:43
1  
@flawr I was imagining the fake language to express arithmetic formulas for real numbers, not act on bits or any sort of finite representation, so I think the measure requirement over the real plane (a,b) ∈ R^2 works fine. But if you actually think it's unclear, I'll edit it. –  xnor Sep 1 at 21:45

3 Answers 3

up vote 9 down vote accepted
+50

23 operations

z = 1/(1/(1/(1/(a+1)+1/(b+1))-1+1/(a+b+1+1))-(1/a+1/b))
res = z+z

proof by explosion:

z = 1/(1/(1/(1/(a+1)+1/(b+1))-1+1/(a+b+1+1))-(1/a+1/b))
             1/(a+1)+1/(b+1)                            == (a+b+2) / (ab+a+b+1)
          1/(1/(a+1)+1/(b+1))                           == (ab+a+b+1) / (a+b+2)
          1/(1/(a+1)+1/(b+1))-1                         == (ab - 1) / (a+b+2)
          1/(1/(a+1)+1/(b+1))-1+1/(a+b+1+1)             == ab / (a+b+2)
       1/(1/(1/(a+1)+1/(b+1))-1+1/(a+b+1+1))            == (a+b+2) / ab
                                              1/a+1/b   == (a+b) / ab
       1/(1/(1/(a+1)+1/(b+1))-1+1/(a+b+1+1))-(1/a+1/b)  == 2 / ab
    1/(1/(1/(1/(a+1)+1/(b+1))-1+1/(a+b+1+1))-(1/a+1/b)) == ab / 2

z = ab / 2 and therefore z+z = ab

score (with added + on subtraction):

z = ////++/++-+/++++-/+/
res = +
share|improve this answer
    
Congrats on the shortest solution! –  xnor Sep 11 at 4:04
    
@xnor thanks for giving me my first accepted answer and my first bounty! –  proud haskeller Sep 11 at 5:33

29 operations

Does not work for the set { (a,b) ∈ R2 | a+b = 0 or a+b = -1 or a-b = 0 or a-b = -1 }. That's probably measure zero?

sum = a+b
nb = -b
diff = a+nb
rfc = 1/(1/(1/sum + -1/(sum+1)) + -1/(1/diff + -1/(diff+1)) + nb + nb)  # rfc = 1/4c
c = 1/(rfc + rfc + rfc + rfc)

# sum  is  2: =+
# nb   is  2: =-
# diff is  2: =+
# rfc  is 18: =///+-/++-//+-/+++
# c    is  5: =/+++
# total = 29 operations

The structure of rfc (Reciprocal-Four-C) is more evident if we define a macro:

s(x) = 1/(1/x + -1/(x+1))              # //+-/+ (no = in count, macros don't exist)
rfc = 1/(s(sum) + - s(diff) + nb + nb) # =/s+-s++ (6+2*s = 18)

Let's do the math:

  • s(x), mathematically, is 1/(1/x - 1/(x+1)) which is after a bit of algebra is x*(x+1) or x*x + x.
  • When you sub everything into rfc, it's really 1/((a+b)*(a+b) + a + b - (a-b)*(a-b) - a + b + (-b) + (-b)) which is just 1/((a+b)^2 - (a-b)^2).
  • After difference of squares, or just plain expansion, you get that rfc is 1/(4*a*b).
  • Finally, c is the reciprocal of 4 times rfc, so 1/(4/(4*a*b)) becomes a*b.
share|improve this answer
2  
+1, I was in the middle of finishing up this identical calculation –  Eric Tressler Sep 1 at 21:19
1  
That's definitely measure zero; it's a union of lines. –  xnor Sep 1 at 21:54
    
Not gonna make a comment about union of lines... @algorithmshark Can you tell us more how you did come up with this identity? How did you aproach the problem? –  flawr Sep 2 at 12:31
1  
@flawr I recalled that the properties of s(x) fit the requirements of the question, from calculus, so that meant I had a square function. After some faffing about, I found I could get an a*b term with the difference of squares trick. Once I had that, it was a matter of trying out which assignments saved operations. –  algorithmshark Sep 2 at 15:22
    
Since you use -1 three times in rfc, couldn't you golf a character out by assigning it to a variable? –  isaacg Sep 2 at 18:23

27 operations

tmp = 1/(1/(1+(-1/(1/(1+(-a))+1/(1+b))))+1/(1/(1/b+(-1/a))+1/(a+(-b))))
res = tmp+tmp+(-1)

# tmp is 23: =//+-//+-+/++///+-/+/+-
# res is 4: =++-

There is no theory behind this. I just tried to get (const1+a*b)/const2 and started with (1/(1-a)+1/(1+b)) and (-1/a+1/b).

share|improve this answer
    
Your tmp is actually 23, making your score 27. Nice find, though. –  algorithmshark Sep 2 at 15:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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