Question

Write a python 3 program to generate a list of the prime numbers less than 50000...

Write a python 3 program to generate a list of the prime numbers less than 50000 using the Sieve of Eratosthenes.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Complete Python Program Code For Printing Prime Numbers < 50000 Using Sieve of Eratosthenes Approach

# Given below is the Sieve of Eratosthenes function for printing all the prime numbers

# less than or equal to a given number 'n' , which in this case is 50000

def sieveOfEratosthenes(n):

    

# In this funciton we first create a boolean array named primeNumbers

# having indices from [0 to n] (both inclusive). This array will represent

# whether the number at any index 'i' is prime or not (i.e. true or false)

# we first initialise all members as True (assuming that all numbers are prime by default)

    primeNumbers = [True for k in range(n + 1)]

    j = 2 # we start with the number 2 since we already know that 0 and 1 are not prime numbers

    while (j * j <= n): # looping while square of the current number that we're checking for is <= our limit (n)

        

        # Now if the current number is still True (i.e. it is prime) then all its next multiples cannot be prime, so we make them False

        if (primeNumbers[j] == True):

            

            # Looping through all multiples of 'j' and making them False as they cannot be prime (j will divide all of them)

            for s in range(j * 2, n + 1, j):

                primeNumbers[s] = False

        j += 1 # Incrementing current 'j' by 1 so as to go to the next number for checking

    primeNumbers[0]= False # Also, at the end, since we know that 0 and 1 are not prime numbers, we make them as False explicitly

    primeNumbers[1]= False

    

# Finally in the same function, we can print all the prime numbers less than input value (n)

    for q in range(n + 1):

        if primeNumbers[q]:

            print (q)


# This below is going to be our Main function (driver code) from where we call the Sieve of Eratosthenes method

n = 50000

print ("All the Prime numbers less than or equal to the given value (",n,") are as follows:- ")

sieveOfEratosthenes(n)

URL Link To Access The Complete Above Code As It Is

https://ide.codingblocks.com/s/183165

Output Screenshots of the Program Result

Add a comment
Know the answer?
Add Answer to:
Write a python 3 program to generate a list of the prime numbers less than 50000...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT