I’m a python noob and have issues setting up a loop that checks if an int z is divisible by a set of numbers, e.g. by 1 – 10. I wrote the following code snippets, but they all return X =[all the numbers of z]… i.e. they fail to apply the if condition so that the mod is checked over all n in a given range/set.
X = []
z = 1
while z in range(1,1000):
if all(z % n == 0 for n in range(1,21)):
X.append(z)
z += 1
also tried:
X = []
if all(i % n == 0 for n in range(1,21)):
X.append(i)
and
X = []
for z in range(1,1000000):
if all(z % n == 0 for n in range(1,21)):
X.append(z)
Any idea what’s going wrong in each of these (or at least 1) of these cases?
Thanks for the help!
seems to work. Second example, i doesn’t seem to be initialized. Third seems to work.
PS : beware that fact(21) is quite big and certainly bigger than 1000000 – it’s not a perfect result but it might be a factor or two below the first answer (prime decomposition yadda yadda)