Problem is as follows:
Create a function that takes 2 inputs, a number
n
and a listlst
. The function should return a list of all the numbers inlst
that share no common factors withn
(other than 1). n and all numbers inlst
will be positive integers greater than or equal to 0.
My attempt
def no_common_factors (n, lst):
def uf(n): #get a set of factors
factors = []
i = 2
while n > 1:
if n % i == 0:
factors += [i]
n = n / i
else:
i += 1
return set(factors)
factors_n = uf(n)
no_common = []
for i in range(0, len(lst)):
factors_i = uf(i)
if factors_n.isdisjoint(factors_i):
no_common += [lst[i]]
else:
continue
return no_common
doesn't work:
In [41]: no_common_factors(15, [72,27,32,61,77,11,40])
Out[41]: [72, 27, 32, 77]
when it should return [32, 61, 77, 11].
I stare at it but can't see what I'm doing wrong, it's supposed to be really simple. Please help!