Click here to Skip to main content
15,125,299 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Create a program that asks the user for an integer greater than or equal to 100. Generate a list of primes up to and including this number. Print the list to the screen on a single line. You may assume that the user will enter valid numeric input in the correct range.​ how to do in python

What I have tried:

def makeprimes( upper ):
    count = 2
    primes = [1]
    while count <= upper:
        prime = True
        for i in range(2, count):
            if count % i == 0:
                prime = False
        if prime:
        count = count + 1
    return primes
Updated 30-Nov-20 10:04am

Create a program which displays primes numbers in sieve of eratosthenes

The problem with your code is that it build a list of primes by using trial division algorithm. And you use the brute force variant.
You need to study the Eratosthenes sieve: Sieve of Eratosthenes - Wikipedia[^]
Sieve of Eratosthenes get the result without resorting to divisions.
CPallini 1-Dec-20 2:10am
Patrice T 1-Dec-20 2:33am
Thank you
Construct an array of all possible numbers in a the range.
Loop through all numbers in the range, eliminating all multiples of that from the array.
What remains are the prime numbers.

Obviously there are quite a few tricks you can do to improve performance here!

But this is your homework, so I'll let you work them out for yourself.
CPallini 1-Dec-20 2:10am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900