I've got this very crude way of writing this IF statement .
for a in range (2,3000):
if ( a % 1) == 0 and ( a % 2) == 0 and ( a % 3) == 0 and ( a % 4) == 0 and ( a % 5) == 0 and ( a % 6) == 0 and ( a % 7) == 0 and ( a % 8) == 0 and ( a % 9) == 0 and ( a % 10) == 0 :
print a
I assume there is a much better way to write this, using for example a range function combined with the IF statement ?
To check if given number is in a range, use Python if statement with in keyword as shown below. number in range() expression returns a boolean value: True if number is present in the range(), False if number is not present in the range. You can also check the other way around using not to the existing syntax.
You can have if statements inside if statements, this is called nested if statements.
Python Nested if statements This is called nesting in computer programming. Any number of these statements can be nested inside one another.
For a more-or-less direct translation, how about
for a in range(2, 3000):
if all(a % k == 0 for k in range(1,11)):
print a
although of course a % 1 == 0 for all integers a, so that check is unnecessary.
What you need is the multiples of LCM(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) which fall within your range. There's multiple ways of computing LCM (see. http://en.wikipedia.org/wiki/Least_common_multiple)
Since LCM(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) = 2^3 * 3^2 * 5 * 7 = 2520, you can do something like
lcm = 2520
i = 2/lcm
j = 3000/lcm
for k in range(i, j)
print (k + 1) * lcm
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