### Introduction

When building an electronics project, a schematic may call for a resistor of an unusual value (say, 510 ohms). You check your parts bin and find that you have no 510-ohm resistors. But you do have many common values above and below this value. By combining resistors in parallel and series, you should be able to approximate the 510-ohm resistor fairly well.

### Task

You must write a function or program which accepts a list of resistor values (resistors you stock) and a target value (which you aim to approximate). The program must consider:

* Individual resistors
* Two resistors in series
* Two resistors in parallel

The program should compute all possible combinations of 1 and 2 resistors from the stock list (including two copies of the same resistor value), compute their series and parallel resistance, then sort the configurations according to how well they approximate the target value.

The output format should be one configuration per line, with a `+` denoting series and `|` denoting parallel, and some space or an = sign before the net resistance.

### Formulas

* The resistance of one resistor is `R1`
* The net resistance of two resistors in series is `R1 + R2`
* The net resistance of two resistors in parallel is `1 / (1/R1 + 1/R2)`
* The distance between an approximated resistance value and the target value can be calculated as pseudo-logarithmic distance, not linear distance: `dist = abs(Rapprox / Rtarget - 1)`. For example, 200 is closer to 350 than it is to 100.
* A better distance measure is true logarithmic distance `dist = abs(log(Rapprox/Rtarget))`, but since this was not specified in the original question, you are free to use either measurement.

### Scoring

Score is measured in characters of code, per usual golf rules. Lowest score wins.

### Example

We have the following resistors in stock `[100, 150, 220, 330, 470, 680, 1000, 1500, 2200, 3300, 4700]` and wish to target `510` ohms. The program should output 143 configurations, approximately as shown (you can change the format, but make sure the meaning is easily determined):

```
680 | 2200     519.444
1000 | 1000    500.
150 + 330      480.
220 + 330      550.
470            470
680 | 1500     467.89
680 | 3300     563.819
100 + 470      570.
220 + 220      440.
100 + 330      430.
470 | 4700     427.273
680 | 4700     594.052
1000 | 1500    600.
470 | 3300     411.406
680 | 1000     404.762
150 + 470      620.
...
many more rows
...
2200 + 4700    6900.
3300 + 4700    8000.
4700 + 4700    9400.

```

In this example, the best approximation of 510 ohms is given by 680- and 2200-ohm resistors in parallel.

**Best of each language so far (1 June 2014):**

1. J - 70 char
2. APL - 102 char
3. Mathematica - 122 char
4. Ruby - 154 char
5. Javascript - 156 char
6. Julia - 163 char
7. Perl - 185 char
8. Python - 270 char
['https://codegolf.stackexchange.com/questions/28595', 'https://codegolf.stackexchange.com', 'https://codegolf.stackexchange.com/users/16615/']
J - 86 71 70 char
=================

```
((]/:[|@<:@%~2{::"1])(;a:,<)"0,[:,/(<,.+`|,.+/;+&.%/)"1@;@((<@,.{:)\))

```

I'm not going to bother to explain every little detail because a lot of the code is spent syncing up the results of different functions, but here's the gist of the golf:

* `;@((<@,.{:)\)` makes every possible pair of resistors, to be connected either in parallel or in series.
* `[:,/(<,.+`|,.+/;+&.%/)"1@` then connects them, in parallel and in series, making a big list of possible connections.
* `(;a:,<)"0,` adds in the possibility of using only one resistor by itself to approximate.
* `(]/:[|@<:@%~2{::"1])` sorts the list of combinations of resistors by the pseudolog distance (`|@<:@%`) between the target and the resultant resistance from each combination.

And this is how to use it:

```
   rouv =: ((]/:[|@<:@%~2{::"1])(;a:,<)"0,[:,/(<,.+`|,.+/;+&.%/)"1@;@((<@,.{:)\))
   # 510 rouv 100 150 220 330 470 680 1000 1500 2200 3300 4700      NB. how many?
143
   10 {. 510 rouv 100 150 220 330 470 680 1000 1500 2200 3300 4700  NB. view first 10
+---------+-+-------+
|680 2200 |||519.444|
+---------+-+-------+
|1000 1000|||500    |
+---------+-+-------+
|150 330  |+|480    |
+---------+-+-------+
|220 330  |+|550    |
+---------+-+-------+
|470      | |470    |
+---------+-+-------+
|680 1500 |||467.89 |
+---------+-+-------+
|680 3300 |||563.819|
+---------+-+-------+
|100 470  |+|570    |
+---------+-+-------+
|220 220  |+|440    |
+---------+-+-------+
|100 330  |+|430    |
+---------+-+-------+

```

You don't have to only view the first 10 like I did above, but this is a function and the J REPL truncates very large return values, and the full output for this example has 287 lines. You can force it all to STDOUT with something like `tmoutput toCRLF , LF ,.~ ": blah rouv blah` on Windows—drop the `toCRLF` on Linux—but `rouv` is a function and internally, all the rows exist.

Note:
-----

The question seems to have been changed right under our noses, and now the log distance is defined as `abs(log(Rapprox/Rtarget))` instead of `abs(Rapprox/Rtarget-1)`. To correct this in my golf, we can change the `|@<:@%` to `|@^.@%`: `<:` is Decrement while `^.` is Logarithm.
Python 3 - ~~250~~ ~~247~~ 270 bytes
====================================

```
from itertools import*
import sys
r=sys.argv[1:]
t=int(r.pop())
p=set(map(tuple,map(sorted,product(r,r))))
a=[('+'.join(b),sum(map(int,b)))for b in p]+[('|'.join(b),1/sum(map(lambda n:1/int(n),b)))for b in p]
for s in sorted(a,key=lambda b:abs(float(b[1])/t-1)):print(s)

```

Run like this:

```
python resistors.py 100 150 220 330 470 680 1000 1500 2200 3300 4700 510

```

(that is, a space-delimited list of resistors, with the target value at the end)

Output:

```
('2200|680', 519.4444444444445)
('1000|1000', 500.0)
('150+330', 480)
('220+330', 550)
('1500|680', 467.88990825688074)
('3300|680', 563.8190954773869)

[snip]

('2200+4700', 6900)
('3300+4700', 8000)
('4700+4700', 9400)

```

~~I would say that outputting, say, `680|2200` and `2200|680` separately is still pretty clear. If this is unacceptable, I can change it, but it'll cost me bytes.~~ Wasn't acceptable. Cost me bytes. Now I sort the tuples before chucking them into the set, otherwise the solution is identical.