0

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
inspectorG4dget
  • 97,394
  • 22
  • 128
  • 222
marc lincoln
  • 1,385
  • 4
  • 20
  • 25
  • is that your real problem? are you really just looking for all multiples of 60? –  Feb 20 '09 at 23:09
  • almost exactly the same code was posted http://stackoverflow.com/questions/567222/simple-prime-generator-in-python and what exactly "Is there any syntax that will calculate what I'm looking for" supposed to mean? – SilentGhost Feb 20 '09 at 23:15
  • First, "while True:" is much more concise. Second, the current code looks broken; for the iterations, x is increasing from 1 to 4 and start is increasing from 1 to 4, but simultaneously. You want to loop independently for each one. – Nikhil Chelliah Feb 20 '09 at 23:52
  • I recognize this example - it is how I used to think about programming when I learned QuickBasic. (Using excessive variables to control status of a loop) – Marc Maxmeister Dec 21 '12 at 15:23

2 Answers2

3

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!

0

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
maayank
  • 4,050
  • 2
  • 21
  • 23