Problem with following Prime Number Generator code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • koolest1
    New Member
    • Jun 2007
    • 7

    Problem with following Prime Number Generator code

    I've been having problem with the following code. It's supposed to
    print the prime numbers between 10 and 100. But i'm not getting any
    output, i.e. i guess the outer 'for' loop is being traversed only
    once. I would be greatful if you could help me out. Thanx![CODE=python]
    >>> f=1
    >>> for i in range(10,100):

    ... for j in range(2,i):
    ... if i%j==0:
    ... f=0
    ... break
    ... else: continue
    ... if f==1:
    ... print i,
    ...[/CODE]
    Last edited by bartonc; Jun 12 '07, 06:00 AM. Reason: Added [CODE=python][CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by koolest1
    I've been having problem with the following code. It's supposed to
    print the prime numbers between 10 and 100. But i'm not getting any
    output, i.e. i guess the outer 'for' loop is being traversed only
    once. I would be greatful if you could help me out. Thanx![CODE=python]
    >>> f=1
    >>> for i in range(10,100):

    ... for j in range(2,i):
    ... if i%j==0:
    ... f=0
    ... break
    ... else: continue
    ... if f==1:
    ... print i,
    ...[/CODE]
    I played with your code a bit, but didn't get anywhere.
    See this discussion on generating a list of prime numbers.

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by koolest1
      I've been having problem with the following code. It's supposed to
      print the prime numbers between 10 and 100. But i'm not getting any
      output, i.e. i guess the outer 'for' loop is being traversed only
      once. I would be greatful if you could help me out. Thanx![CODE=python]
      >>> f=1
      >>> for i in range(10,100):

      ... for j in range(2,i):
      ... if i%j==0:
      ... f=0
      ... break
      ... else: continue
      ... if f==1:
      ... print i,
      ...[/CODE]
      I think your problem is you never set 'f' back to one again so it will always be zero after the 10, which isn't prime. Try this:
      [code=python]
      for i in range(10, 100):
      for j in range(2, i):
      if not i % j:
      break

      else:
      print i
      [/code]
      As you see, it doesn't use an unnecessary variable.

      Comment

      • Smygis
        New Member
        • Jun 2007
        • 126

        #4
        I like the Sieve of Eratosthenes. Not the fastest algorithm around, But it can be made wery clean in python:

        [code=python]
        >>> primes = range(10,100)
        >>> for i in range(2, 8):
        ... primes = filter(lambda x: x == i or x % i, primes)
        ...
        >>> print primes
        [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
        >>>
        [/code]

        Comment

        Working...