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

  • SEARCH
  • Home
  • 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 6160801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:23:18+00:00 2026-05-23T21:23:18+00:00

Am trying to write a piece of python code that calculate and print 1000

  • 0

Am trying to write a piece of python code that calculate and print 1000 prime number from 2
however i only got 1999 as the last element in my result. I know there a lot of questions like this asked before but I wanna know why is my code is not working.
btw: how do i declare a boolean value in python? cant find a clue even googled···sad

mylist=[2]
num=1
count=0
while count<1000:
    if num>1:
        add=1
        for i in mylist:
            if num%i==0:
                add=0
                break
        if add==1:
            mylist=mylist+[num]
    num=num+2
    count=count+1
print mylist
  • 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-23T21:23:19+00:00Added an answer on May 23, 2026 at 9:23 pm

    Your loop should read while len(mylist) < 1000:, and remove all references to count. Either that, or you should only increment count every time you add a prime to the list.

    Also, you don’t ‘declare’ values in Python. You simply assign a value of the desired type to a variable, and poof, you have a variable of that type. In this case, True and False are boolean values and you can assign them to variables.

    Here is my version in idiomatic (i.e. written the way an experienced Python programmer might write it) Python:

    primes = [2]
    candidate = 3
    while len(primes) < 1000:
        isprime = True
        for testprime in primes:
            if candidate % testprime == 0:
                isprime = False
                break
        if isprime:
            primes.append(candidate)
        candidate = candidate + 2
    print primes
    

    If you want to write it in even terser and faster Python, do this:

    import itertools
    primes = [2]
    candidate_iter = itertools.count(3, 2)
    while len(primes) < 1000:
        candidate = candidate_iter.next()
        if all(candidate % testprime != 0 for testprime in primes):
            primes.append(candidate)
    print primes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a piece of code that will do the following: Take
I'm trying to write a unit test for a piece of code that generates
I'm trying to write a piece of code that reads a file line by
I'm trying to write a piece of code in python to get command-line options
I am trying to write a piece of code that can parse any xml
I'm trying to write a small Python script that will get query results from
I'm trying to write a small piece of code that passes a small formula
I am trying to write a python script that benchmarks several pieces of code.
Begginer's question. I'm trying to write small piece of code in Visual Studio (VB.net)
im trying to write an app that will display a list off lines from

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.