Reputation: 1571
I want to find all the numbers divisble by all the numbers between 1 and 5. how do I write the program so that if the remainder of 'start' divided by all the numbers that x goes through is equal to 0 that it will print start. Is there any syntax that will calculate what I'm looking for. thanks.
import math
def main():
one = 1
start = 1
while one == 1:
for x in range(1, 5):
if start % x == 0:
print start
start += 1
Upvotes: 0
Views: 313
Reputation: 4250
if I understood correctly you want something like this:
start = 1
while (True):
flgEvenlyDiv = True
for x in range(1, 5):
if (start % x != 0):
flgEvenlyDiv = False
break
if (flgEvenlyDiv == True):
print start
start += 1
Upvotes: 0
Reputation:
First of all, you seem to ask for all multiples of 60. Those can be rendered easily like this (beware, this is an infinite loop):
from itertools import count
for i in count():
print i*60
If you just oversimplified your example, this is a more pythonic (and correct) solution of what you wrote (again an infinite loop):
from itertools import count
# put any test you like in this function
def test(number):
return all((number % i) == 0 for i in range(1,6))
my_numbers = (number for number in count() if test(number))
for number in my_numbers:
print number
You had a grave bug in your original code: range(1,5)
equals [1, 2, 3, 4]
, so it would not test whether a number is divisble by 5!
PS: You have used that insane one = 1
construct before, and we showd you how to code that in a better way. Please learn from our answers!
Upvotes: 3