Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6592343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:32:33+00:00 2026-05-25T17:32:33+00:00

I wrote a piece of code to print n prime numbers: class PrimeGen: def

  • 0

I wrote a piece of code to print n prime numbers:

class PrimeGen:
    def __init__(self):
        self.current = 2
    def genPrime(self, num):
        for i in range(num):
            while 1:
                for j in range(2, self.current/2 + 1):
                    if self.current % j == 0:
                        self.current = self.current + 1
                        break
                else:
                    break
            print self.current,
            self.current = self.current + 1

if __name__ == '__main__':
    p = PrimeGen()
    p.genPrime(5)

The code works fine. I get 2 3 5 7 11 as output. I tried to make the class iterable. Code below. But the output is 0 1 2 3 4. I could not quite figure out where I am going wrong. Any help is appreciated. Thanks

class PrimeIter:
    def __init__(self):
        self.current = 1

    def next(self):
        self.current = self.current + 1
        while 1:
            for i in range(2, self.current/2 + 1):
                if self.current % i == 0:
                    self.current = self.current + 1
                    break # Break current for loop
            else:
                break # Break the while loop and return
        return self.current

    def __iter__(self):
        return self

if __name__ == '__main__':
    p = PrimeIter()
    for p in range (5):
        print p,
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:32:34+00:00Added an answer on May 25, 2026 at 5:32 pm

    You’re using this code to print out the values:

    for p in range (5):
        print p,
    

    If you look at that, it’s printing the values of the range. You probably want to print things from the prime iterator. itertools has some functions that may help:

    for prime in itertools.islice(p, 5):
        print prime,
    

    Additionally, you may want to consider using a generator:

    def primes():
        current = 1
        while True:
            current += 1
            while True:
                for i in xrange(2, current // 2 + 1):
                    if current % i == 0:
                        current += 1
                        break
                else:
                    break
            yield current
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a little piece of code to expose my problem. public class date
I wrote this piece of code that lists all JPG files in the current
Following is the piece of code that i wrote to calculate the nth prime
I wrote this piece of code that is supposed to redirect something written on
I've wrote this simple piece of code. And I have a slight problem with
I wrote a same(..similar?) piece of code for Java 7 and C#(.net 3.5) and
I have a program I wrote in Windows with this piece of code that
I have a very simple little piece of Lua code, which I wrote while
This is a simple piece of code which i wrote to check whether it
Here's a little piece of code I wrote to report variables with missing values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.