Is the Circuit Possible?



Write a program or function that takes input: all resistors available and a resistance value and outputs a truthy value of whether or not it is possible to get the resistance by using those resistors.


Any format for input will do.

There will be at least 1 available resistor and your program should output for at least 10 available resistors.

Resistance of all available resistors and the required resistance will be positive integers.

For available resistors if a fractional value is also possible, the required resistance may be the approximated value.(See example)

Output should be any 2 unique values for Possible and Not possible.

The resistors may be connected in any manner.

Series Resistance: For n resistances in series: Result=R1+R2+R3+....Rn

Parallel Resistance: For n resistances in parallel: Result=1/(1/R1+1/R2+1/R3+....+1/Rn)

The circuit may not require all the resistors to get the required resistance (output True if that is the case).


This is code-golf so shortest-code wins.


R     List
110   220,220 -> True
440   220,220 -> True
550   400,300 -> False
3000  1000,3000 -> True
750   1000,3000 -> True
333   1000,1000,1000 -> True (1000||1000||1000=333.333)
667   1000,1000,1000 -> True ((1000+1000)||1000=666.6666)
8000  1000,1000,7000 -> True 
190   100,200,333,344,221 -> True 
193   105,200,333,344,221 -> True
400   200,100 -> False

Explanation for the last two examples:

Vedant Kandoi

Posted 2018-10-29T06:46:59.677

Reputation: 1 955

What bout 3 3 1, 3 3 2? – l4m2 – 2018-10-29T08:05:27.047

1.5 is rounded to 2, required resistance will not be given 0(added it to the question), 3 3 will be true – Vedant Kandoi – 2018-10-29T08:08:30.170

Nice challenge, but I deal enough with EE as it is... – None – 2018-10-29T21:10:35.130

I guess Machematica win? – l4m2 – 2018-10-31T10:13:47.407

Would you mind adjusting your test cases to more clearly separate the list of resistors from the target resistance for each example? – Kamil Drakari – 2018-10-31T13:57:47.807

Also, suggested test case: [10,200], 400 -> False assuming you don't want to let answers use the same resistor multiple times. – Kamil Drakari – 2018-10-31T15:20:23.137

2This problem is harder than the description makes it sound because general resistor circuits can't be broken down recursively into series and parallel parts, in more complicated ways than the last two test cases. 10 resistors should be easily enough to make such examples. I suspect that none of the currently posted answers work in general, and a correct answer needs to have matrix inversion in some form. – xnor – 2018-10-31T17:04:36.003

If you intend to allow arbitrary circuits, you probably need a description of Kirchhoff's laws. – Nitrodon – 2018-10-31T17:34:02.153

Rounding <0.5 down I assume, so three 1 Ohm resistors in parallel results in a superconductor? – Penguino – 2018-10-31T21:10:25.127

@Penguino Technically yes, but the required resistance will never be 0 in the problem. – Vedant Kandoi – 2018-11-01T05:47:44.270



Python 3, 253 bytes

import functools
def p(l): return functools.reduce(lambda z,x:z+[y+[x]for y in z],l,[[]])
print(l[-1:][0]in[round(a)for a in[sum(a)for a in p([sum(a)for a in p(l)]+[1/sum(c)for c in[[1/b for b in a if b!=0]for a in p(l).copy()]if sum(c)!=0])]])

I take the powerset of all resistor values and then calculate the sums for series and 1/sum(1/values) for paralel and then I take a powerset of these two sets. When you take the sum of all the subsets and put them into a set then this set eiter contains the value or not. -> return True/False

@stephen thanks :)


Posted 2018-10-29T06:46:59.677

Reputation: 11

2Welcome to PPCG! If you need any imports, they need to be included in your code. Additionally, you need to take inputs yourself, rather than assuming the input is in a variable. Also, b != 0 -> b!=0. – Stephen – 2018-10-31T13:19:29.250

As Stephen saod, you can't take input through a pre-defined variable, otherwise this is a snippet, which is not allowed. You should change it to either a function, or a full program – Jo King – 2018-10-31T13:28:54.610


It doesn't work for the third test case (also, golfed a bit and with proper input. if you're worried I broke something, your original code doesn't work either)

– Jo King – 2018-10-31T13:36:43.800

dammit. Ill have a look :( – SimonSchuler – 2018-10-31T13:38:19.883


Japt, 52 bytes

à má

ÈÊ<2?X:X¯1 ïXÅgW @[X+Y1/(1/XÄ/Y]Ãc
mmW c mr øV

Try it!

This was a tough one, and I had to do a couple of strange things to make it work. I can't mathematically prove that this works for everything, but it works for all the test cases as well as my extra proposed test case. Specifically, I know that the function I define called W gives different results depending on the order of the resistors in its input so I run it on each possible ordering of each possible combination of resistors. I also know that it will produce a list of resistances that are all possible to create using the input resistors. I don't know with 100% certainty that those two things together end up with every possible resistance.


à       Get each combination e.g. [1,2,3] -> [[1,2,3],[1,2],[1,3],[2,3],[1],[2],[3]]
  m     For each combination...
   á    Get each order e.g. [1,2,3] -> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

    Skip a line to avoid overwriting the target resistance

È                                     Define a function W taking an array input X:
 Ê<2?X                                  If X is only 1 resistor, return it as-is
            XÅgW                        Get the resistances from applying W to the tail of X
       X¯1 ï                            Pair the first resistor with each possible tail resistance
                 @[            ]Ã       For each pair get these two values:
                   X+Y                    Those resistances in series
                      1/(1/XÄ/Y           Those resistances in parallel
                                 c      Collapse all those resistances into a single flat list

mmW            For each combination and order, get the resistances using W
    c          Collapse to a single list of resistances
      mr       Round each one
         øV    Return true if the target resistance is in the list, false otherwise

Kamil Drakari

Posted 2018-10-29T06:46:59.677

Reputation: 3 461


Ruby, 153 bytes


Try it online!

Brute force. I mean it.


Posted 2018-10-29T06:46:59.677

Reputation: 11 099