I have a line of code in my program that checks if a number is divisible by several numbers.
However, the way I currently have it is extremely inefficient and ugly.
It currently looks like:
if(x % 1 == 0 and x % 2 == 0 and x % 3 == 0)
and so forth for several other numbers. I was hoping someone could teach me how I could take out the long and
chain and help me replace it with something that makes more sense so instead it will look like:
if(x % a[1,2,3] == 0)
Is this possible and if so can anyone please help me?
EDIT: The x % 1 == 0, x % 2 == 0, etc.. Is not the full equation. I am checking far more than % 1,2, and 3. I am looking for a solution that can take say.. 1 through 15. That is why I am not using x % 6.
if all(x % k == 0 for k in [1, 2, 3]):
print 'yay'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With