电路中的并联电阻


20

介绍:

两个电阻器,R1并且R2,在平行(表示R1 || R2)具有组合的电阻Rp给定为:

RP2=R1R2R1+R2
或在注释中建议:

RP2=11R1+1R2

三个电阻器,R1R2R3在平行于(R1 || R2 || R3)具有组合的电阻(R1 || R2) || R3 = Rp || R3

RP3=R1R2R1+R2R3R1R2R1+R2+R3

或者,再次如注释中所建议:

RP3=11R1+1R2+1R3

这些公式当然可以扩展到无限数量的电阻器。


挑战:

以正电阻值列表作为输入,如果将它们并联放置在电路中,则输出组合电阻。您可能没有假定电阻的最大数量(当然,您的计算机可以处理的除外)。

测试用例:

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  

每种语言中最短的代码将获胜。强烈建议您进行解释。


6
还有其他一些挑战涉及泛音平均值(1 2 3),但我认为没有重复项。与骗子的建议相符,我认为这个挑战机构应在某处列出该短语,以便我们可以更轻松地关闭将来的骗子。
FryAmTheEggman

Answers:


13

05AB1E5 3字节

zOz

在线尝试!


说明

z                     # compute 1/x for each x in input 
 O                    # sum input 
  z                   # compute 1/sum

4
除了内置的,这可能是我们所能达到的最低水平!


9

MATLAB,14个字节

在MATLAB中,norm(...,p)计算p向量的- 范数。这通常是用于定义p1作为

vp=(i|vi|p)1p.

但幸运的是,它也适用于p=1。(请注意,它在八度中不起作用。)

@(x)norm(x,-1)

不要在线尝试!


4
这既恐怖又美丽!
停止转动逆时针

1
谢谢,这些是最好的赞美:)
瑕疵的

7

果冻 5  3 字节

İSİ

在线尝试!

怎么样?

最初,我在电子工程时代就忘记了此表格...我们多么容易忘记。

İSİ - Link: list of numbers, R   e.g. [r1, r2, ..., rn]
İ   - inverse (vectorises)            [1/r1, 1/r2, ..., 1/rn]
 S  - sum                             1/r1 + 1/r2 + ... + 1/rn
  İ - inverse                         1/(1/r1 + 1/r2 + ... + 1/rn)

4
我假设İ是读音相同,i在发音list。这是说挑战很容易的一种方式吗?
Stewie Griffin







3

Perl 6、14个字节

1/*.sum o 1/**

在线尝试!

1 / ** is an anonymous function that returns a list of the reciprocals of its arguments. 1 / *.sum is another anonymous function that returns the reciprocal of the sum of the elements of its list argument. The o operator composes those two functions.


Very nice. I don't see HyperWhatevers used often enough in golfing since they can't be used in more complex expressions. If they were closer to normal Whatevers, I'd expect sumething like this to work, but alas...
Jo King

Yeah, this is probably the first time I've even thought about using one for golfing, and I was disappointed to discover its limitations.
Sean



3

MathGolf, 3 bytes

∩Σ∩

The same as other answers, using the builtins (1n) and Σ (sum):

M(x1,...,xn)=11x1+1x2+...+1xn

Try it online.


2

PHP, 51 bytes

Reciprocal of sum of reciprocals. Input is $a.

1/array_reduce($a,function($c,$i){return$c+1/$i;});

Try it online!


With PHP7.4, I think you can do this: 1/array_reduce($a,fn($c,$i)=>$c+1/$i); (38 bytes). Read more in wiki.php.net/rfc/arrow_functions
Ismael Miguel

I think you're right! But nowhere to demo?

You have to download it yourself. However, since PHP 7.4.0RC1 was released on the 5th of this month (php.net/archive/2019.php#2019-09-05-1), you probably are safe using it. If you have doubts, you can ask in the meta.
Ismael Miguel




2

x86-64 Machine code - 20 18 bytes

0F 57 C0             xorps       xmm0,xmm0  
loopHead
F3 0F 53 4C 8A FC    rcpss       xmm1,dword ptr [rdx+rcx*4-4]
0F 58 C1             addps       xmm0,xmm1  
E2 F6                loop        loopHead
0F 53 C0             rcpps       xmm0,xmm0  
C3                   ret  

Input - Windows calling convention. First parameter is the number of resistors in RCX. A pointer to the resistors is in RDX. *ps instructions are used since they are one byte smaller. Technically, you can only have around 2^61 resistors but you will be out of RAM long before then. The precision isn't great either, since we are using rcpps.


“Only 2⁶¹ resistors” would probably fill the observable universe (many times over)!

Actually, 2^61 is only 2.305843e+18 and the observable universe is 8.8 × 10^26 m in diameter.
me'

Yeah, serious overestimation! Actual magnitude would be around the size and mass of Deimos, smaller moon of Mars.

2

Java 8, 24 bytes

a->1/a.map(d->1/d).sum()

I noticed there wasn't a Java answer yet, so figured I'd add one.

Try it online.

Explanation:

Uses the same Harmonic Mean approach as other answers:

M(x1,...,xn)=11x1+1x2+...+1xn

a->                       // Method with DoubleStream parameter and double return-type
     a.map(d->1/d)        //  Calculate 1/d for each value `d` in the input-stream
                  .sum()  //  Then take the sum of the mapped list
   1/                     //  And return 1/sum as result

2

MATL, 5 bytes

,1w/s

Try it online!

I'm not sure if "do twice" (,) counts as a loop, but this is just the harmonic mean, divided by n.

Alternately, ,-1^s is five bytes as well.


2

Intel 8087 FPU machine code, 19 bytes

 D9 E8      FLD1                    ; push 1 for top numerator on stack
 D9 EE      FLDZ                    ; push 0 for running sum 
        R_LOOP: 
 D9 E8      FLD1                    ; push 1 numerator for resistor
 DF 04      FILD WORD PTR[SI]       ; push resistor value onto stack 
 DE F9      FDIV                    ; divide 1 / value 
 DE C1      FADD                    ; add to running sum 
 AD         LODSW                   ; increment SI by 2 bytes 
 E2 F4      LOOP R_LOOP             ; keep looping 
 DE F9      FDIV                    ; divide 1 / result                  
 D9 1D      FSTP WORD PTR[DI]       ; store result as float in [DI]

This uses the stack-based floating point instructions in the original IBM PC's 8087 FPU.

Input is pointer to resistor values in [SI], number of resistors in CX. Output is to a single precision (DD) value at [DI].


1

Dart, 42 bytes

f(List<num>a)=>a.reduce((p,e)=>p*e/(p+e));

Try it online!

Having to explicitly specify the num type is kinda sucky, prevents type infering, because it would infer to (dynamic, dynamic) => dynamic which can't yield doubles for some reason



1

Python 3, 58 44 bytes

f=lambda x,y=0,*i:f(x*y/(x+y),*i)if y else x

A recursive function. Requires arguments to be passed unpacked, like so:

i=[10, 10, 20]
f(*i)

or

f(10, 10, 20)

Explanation:

# lambda function with three arguments. *i will take any unpacked arguments past x and y,
# so a call like f(10, 20) is also valid and i will be an empty tuple
# since y has a default value, f(10) is also valid
f=lambda x,y=0,*i: \

# a if case else b
# determine parallel resistance of x and y and use it as variable x
# since i is passed unpacked, the first item in the remaining list will be y and
# the rest of the items will be stored in i
# in the case where there were no items in the list, y will have the default value of 0
f(x*y/(x+y),*i) \

# if y does not exist or is zero, return x
if y else x

1

Charcoal, 7 bytes

I∕¹Σ∕¹A

Try it online! Link is to verbose version of code. Works by calculating the current drawn by each resistor when 1V is applied, taking the total, and calculating the resistance that would draw that current when 1V is applied. Explanation:

      A Input array
    ∕¹  Reciprocal (vectorised)
   Σ    Sum
 ∕¹     Reciprocal
I       Cast to string for implicit print


1

[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.


1

Forth (gforth), 49 bytes

: f 0e 0 do dup i cells + @ s>f 1/f f+ loop 1/f ;

Try it online!

Input is a memory address and array length (used as an impromptu array, since Forth doesn't have a built-in array construct)

Uses the sum-of-inverse method as most other answers are

Code Explanation

: f           \ start a new word definition
  0e          \ stick an accumulator on the floating point stack
  0 do        \ start a loop from 0 to array-length -1
    dup       \ copy the array address
    i cells + \ get the address of the current array value
    @ s>f     \ get the value and convert it to a float
    1/f f+    \ invert and add to accumulator
  loop        \ end the loop definition
  1/f         \ invert the resulting sum
;             \ end the word definition

1

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.
<recently read> }

l.3 \1{1, 1}

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

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

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

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

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

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.
<recently read> }

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}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.