# Parallel resistance in electric circuits

### Introduction:

Two resistors, R1 and R2, in parallel (denoted R1 || R2) have a combined resistance Rp given as:

$$R_{P_2} = \frac{R_1\cdot R_2}{R_1+R_2}$$ or as suggested in comments:

$$R_{P_2} = \frac{1}{\frac{1}{R_1} + \frac{1}{R_2}}$$

Three resistors, R1, R2 and R3 in parallel (R1 || R2 || R3) have a combined resistance (R1 || R2) || R3 = Rp || R3 :

$$R_{P_3} = \frac{\frac{R_1\cdot R_2}{R_1+R_2}\cdot R_3}{\frac{R_1\cdot R_2}{R_1+R_2}+R_3}$$

or, again as suggested in comments:

$$R_{P_3} = \frac{1}{\frac{1}{R_1} + \frac{1}{R_2}+ \frac{1}{R_3}}$$

These formulas can of course be extended to an indefinite number of resistors.

### Challenge:

Take a list of positive resistor values as input, and output the combined resistance if they were placed in parallel in an electric circuit. You may not assume a maximum number of resistors (except that your computer can handle it of course).

### Test cases:

1, 1
0.5

1, 1, 1
0.3333333

4, 6, 3
1.3333333

20, 14, 18, 8, 2, 12
1.1295

10, 10, 20, 30, 40, 50, 60, 70, 80, 90
2.6117


Shortest code in each language wins. Explanations are highly encouraged.

• I think it would be much clearer to write $R_p = \frac{1}{\frac{1}{R_1} + \frac{1}{R_2}$, which extends more easily to the case of an arbitrary number of resistors. – flawr Sep 11 at 14:56
• There are a few other challenges that refer to the harmonic mean (1 2 3) but I don't think there is a duplicate. In line with what flawr suggested, I think this challenge body should have that phrase listed somewhere so we can close a future dupe more easily. – FryAmTheEggman Sep 11 at 15:17
• Obligatory XKCD – Mast Sep 12 at 6:25
• "You may not assume a maximum number of resistors." May we at least assume the amount fits in a 32-bit int? – Mast Sep 12 at 6:29
• Better hope the resistor list fits in memory.... – Jacco van Dorp Sep 12 at 6:56

# Japt v2.0a0, 7 bytes

1÷Ux!÷1


Try it

# OCaml, 50 bytes

fun l->1./.(List.fold_left(fun a e->a+.1./.e)0. l)


Try it online!

New contributor
Saswat Padhi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

# Stax, 5 bytes

{u+Fu


Run and debug it at staxlang.xyz!

{  F     For each:
u         Invert
u    Invert
Implicit print as fraction


# Perl 5 (-p), 17 bytes

$a+=1/$_}{$_=1/$a


Try it online!

# [MATLAB], 15 bytes

One more byte than flawr excellent answer, but I had to use other functions so here goes:

@(x)1/sum(1./x)


It's rather explicit, it sums the inverse of the resistances, then invert the sum to output the equivalent parallel resistance.

# Scala, 15 bytes

1/_.map(1/).sum


Try it online!

# expl3 (LaTeX3 programming layer), 65 bytes

The following defines a function that prints the result to the terminal (unfortunately expl3 has very verbose function names):

\def\1#1{\fp_show:n{1/(\clist_map_function:nN{#1}\2)}}\def\2{+1/}


A complete script which can be run from the terminal including all the test cases as well as the setup to enter expl3:

\RequirePackage{expl3}\ExplSyntaxOn
\def\1#1{\fp_show:n{1/(\clist_map_function:nN{#1}\2)}}\def\2{+1/}
\1{1, 1}
\1{1, 1, 1}
\1{4, 6, 3}
\1{20, 14, 18, 8, 2, 12}
\1{10, 10, 20, 30, 40, 50, 60, 70, 80, 90}
\stop


If run with pdflatex <filename> the following is the console output:

This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./cg_resistance.tex
LaTeX2e <2018-12-01>
(/usr/local/texlive/2019/texmf-dist/tex/latex/unravel/unravel.sty
(/usr/local/texlive/2019/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2019/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/local/texlive/2019/texmf-dist/tex/latex/l3backend/l3backend-pdfmode.def))
(/usr/local/texlive/2019/texmf-dist/tex/latex/l3packages/xparse/xparse.sty)
(/usr/local/texlive/2019/texmf-dist/tex/generic/gtl/gtl.sty))
> 1/(\clist_map_function:nN {1,1}\2)=0.5.

l.3 \1{1, 1}

?
> 1/(\clist_map_function:nN {1,1,1}\2)=0.3333333333333333.

l.4 \1{1, 1, 1}

?
> 1/(\clist_map_function:nN {4,6,3}\2)=1.333333333333333.

l.5 \1{4, 6, 3}

?
> 1/(\clist_map_function:nN {20,14,18,8,2,12}\2)=1.129538323621694.

l.6 \1{20, 14, 18, 8, 2, 12}

?
> 1/(\clist_map_function:nN
{10,10,20,30,40,50,60,70,80,90}\2)=2.611669603067675.

l.7 \1{10, 10, 20, 30, 40, 50, 60, 70, 80, 90}

?
)
No pages of output.
Transcript written on cg_resistance.log.


## Explanation

\fp_show:n : evaluates its argument as a floating point expression and prints the result on the terminal, every expandable macro is expanded during that process.

\clist_map_function:nN : takes two arguments, a comma separated list and a function/macro, if called like \clist_map_function:nN { l1, l2, l3 } \foo it expands to something like \foo{l1}\foo{l2}\foo{l3}. In our case instead of \foo the macro \2 is used, which expands to +1/ so that the expression expands to +1/{l1}+1/{l2}+1/{l3}