There's a 500 rep unofficial bounty for beating the current best answer.
Goal
Your goal is to multiply two numbers using only a very limited set of arithmetic operations and variable assignment.
- Addition
x,y -> x+y
- Reciprocal
x -> 1/x
(not divisionx,y -> x/y
) - Negation
x -> -x
(not subtractionx,y -> x-y
, though you can do it as two operationsx + (-y)
) - The constant
1
(no other constants allowed, except as produced by operations from1
) - 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 atomic-code-golf. 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.)