### 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/']
APL (102)
=========

```
{V←{⊃¨⍺{⍺,⍺⍺,⍵,'=',⍺⍵⍵⍵}⍺⍺/¨Z/⍨≤/¨Z←,∘.,⍨⍵}⋄K[⍋|¯1+⍺÷⍨0 4↓K←↑('|'{÷+/÷⍺⍵}V⍵),('+'+V⍵),{⍵,'  =',⍵}¨⍵;]}

```

This takes the target resistance as the left argument and a list of available resistors as the right argument.

Explanation:

* `V←{`...`}`: `V` is a function that:
	+ `Z/⍨≤/¨Z←,∘.,⍨⍵`: finds every unique combination of two values in `⍵`,
		- `Z←,∘.,⍨⍵`: join each value in `⍵` with each value in `⍵`, store in `Z`,
		- `Z/⍨≤/¨Z`: select from `Z` those combinations where the first value is less than or equal to the second value
	+ `⍺{`...`}⍺⍺/¨`: and then applies following function, bound with the left function (`⍺⍺`) on the right and the left argument (`⍺`) on the left, to each pair:
		- `⍺,⍺⍺,⍵,'=',⍺⍵⍵⍵`, the left argument, followed by the left bound argument, followed by the right argument, followed by `=`, followed by the right function (`⍵⍵`) applied to both arguments. (This is the formatting function, `X [configuration] Y [equals] (X [fn] Y)`.)
	+ `⊃¨`: and then unbox each element.
* `{⍵,' =',⍵}¨⍵`: for each element in `⍵`, make the configurations for the individual resistors. (`⍵`, nothing, nothing, `=`, `⍵`).
* `('+'+V⍵)`: use the `V` function to make all serial configurations (character is `'+'` and function is `+`).
* `'|'{÷+/÷⍺⍵}V⍵`: use the `V` function to make all parallel configurations (character is `'|'` and function is `{÷+/÷⍺⍵}`, inverse of sum of inverse of arguments).
* `K←↑`: make this into a matrix and store it in `K`.
* `0 4↓K`: drop the 4 first columns from `K`, leaving only the resistance values.
* `|¯1+⍺÷⍨`: calculate the distance between `⍺` and each configuration.
* `K[⍋`...`;]`: sort `K` by the distances.
Julia - ~~179~~ 163 bytes
=========================

```
f(t,s)=(\ =repmat;m=endof(s);A=A[v=(A=s\m).>=(B=sort(A))];B=B[v];F=[s,C=A+B,A.*B./C];n=sum(v);print([[s P=[" "]\m P;A [+]\n B;A [|]\n B] F][sortperm(abs(F-t)),:]))

```

This works the same as the old version, but the argument in the print statement has been organised slightly differently to reduce the number of square brackets necessary. Saves 4 bytes. Absorbing the spaces vector creation into the print argument saves an extra 2 bytes. It has also switched from using "find" to get the relevant indices to using the logical form. Saves 6 bytes. Absorbing the calculation of the index vector into the adjustment of A saved another 2 bytes. Finally, replacing endof(v) with sum(v) saved 2 more bytes. Total saving: 16 bytes.

Old version:

```
f(t,s)=(\ =repmat;m=endof(s);A=s\m;v=find(A.>=(B=sort(A)));A=A[v];B=B[v];F=[s,C=A+B,A.*B./C];n=endof(v);P=[" "]\m;print([[s,A,A] [P,[+]\n,[|]\n] [P,B,B] F][sortperm(abs(F-t)),:]))

```

Within the function, here's what it's doing:

```
\ =repmat            # Overloads \ operator to save lots of characters
m=endof(s)           # Length of input s ("Stock")
A=s\m                # Equivalent to repmat(s,m) (see first command)
B=sort(A)            # Same as A but sorted - rather than cycling through
                     # the resistors m times, it repeats each one m times
v=find(A.>=B)        # Identify which pairs for A,B have A>=B
A=A[v];B=B[v]        # Remove pairs where A<B (prevents duplicates)
F=[s,C=A+B,A.*B./C]  # Constructs vector containing results for single resistor,
                     # resistors in series, and resistors in parallel
n=endof(v)           # equivalent to n=(m+1)m/2, gets number of relevant pairs
P=[" "]\m            # Construct array of blank entries for use in constructing output
print([[s,A,A] [P,[+]\n,[|]\n] [P,B,B] F][sortperm(abs(F-t)),:]))
# The following are the components of the argument in the print statement:
[s,A,A]              # Set of resistor values for resistor 1
[P,[+]\n,[|]\n]      # Operator column, prints either nothing, +, or |
[P,B,B]              # Set of resistor values for resistor 2 (blank for single resistor)
F                    # Contains resulting equivalent resistance
[sortperm(abs(F-t)),:] # Determines permutation for sorting array by distance from Target t
                     # and applies it to array

```

Sample output:

```
julia> f(170,[100,220,300])
300  |  300  150
100  +  100  200
300  |  220  126.92307692307692
220          220
220  |  220  110
100          100
300  |  100  75
220  |  100  68.75
100  |  100  50
300          300
220  +  100  320
300  +  100  400
220  +  220  440
300  +  220  520
300  +  300  600

```