Simple prime number generator in Python

0 votes

I want a  simple prime generator. This is the code I have:

import math

def main():
    count = 3
    one = 1
    while one == 1:
        for x in range(2, int(math.sqrt(count) + 1)):
            if count % x == 0: 
                continue
            if count % x != 0:
                print count

        count += 1

But it's just printing 'count'. Can someone help me with this?

Apr 25 in Python by Kichu
• 19,040 points
26 views

1 answer to this question.

0 votes

There are some problems with your code.

  • You are printing count without dividing it by X.
  • You are using break instead of continue, break stops the iteration, and continue moves to the next iteration.

Here is the working code :

import math


def main():

    count = 3

    

    while True:

        isprime = True

        

        for x in range(2, int(math.sqrt(count) + 1)):

            if count % x == 0: 

                isprime = False

                break

        

        if isprime:

            print count

        

        count += 1

For a much more efficient prime generator use this :

# Sieve of Eratosthenes

# Code by David Eppstein, UC Irvine, 28 Feb 2002

# http://code.activestate.com/recipes/117119/


def gen_primes():

    """ Generate an infinite sequence of prime numbers.

    """

    # Maps composites to primes witnessing their compositeness.

    # This is memory efficient, as the sieve is not "run forward"

    # indefinitely, but only as long as required by the current

    # number being tested.

    #

    D = {}

    

    # The running integer that's checked for primeness

    q = 2

    

    while True:

        if q not in D:

            # q is a new prime.

            # Yield it and mark its first multiple that isn't

            # already marked in previous iterations

            # 

            yield q

            D[q * q] = [q]

        else:

            # q is composite. D[q] is the list of primes that

            # divide it. Since we've reached q, we no longer

            # need it in the map, but we'll mark the next 

            # multiples of its witnesses to prepare for larger

            # numbers

            # 

            for p in D[q]:

                D.setdefault(p + q, []).append(p)

            del D[q]

        

        q += 1

I hope this helps you.

answered Apr 25 by narikkadan
• 10,640 points

Related Questions In Python

0 votes
2 answers

Check a number is Prime or not in Python

There is an efficient way to write ...READ MORE

answered Oct 26, 2018 in Python by Priyaj
• 58,080 points
2,657 views
0 votes
1 answer

What is the logic to check if a number is prime or not in python?

n = int (input ('ENTER NUMBER TO ...READ MORE

answered Jul 16, 2020 in Python by amrut
• 240 points
338 views
0 votes
1 answer

Number division in python

For Python 3, use the // operator: q = ...READ MORE

answered Apr 18, 2018 in Python by Nietzsche's daemon
• 4,260 points
247 views
0 votes
2 answers

How to calculate square root of a number in python?

calculate square root in python >>> import math ...READ MORE

answered Apr 2, 2019 in Python by anonymous
3,049 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,812 views
0 votes
1 answer
0 votes
1 answer

Programmatically generate video or animated GIF in Python?

You can use  ImageMagick. Save your frames ...READ MORE

answered Apr 25 in Python by narikkadan
• 10,640 points
26 views
0 votes
1 answer

ln (Natural Log) in Python

Use math.log it is a natural logarithm. For ...READ MORE

answered Apr 25 in Python by narikkadan
• 10,640 points
57 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP