Question

Use phyton TheSieve of Eratosthonesis an algorithm to find all the prime numbers between 1 andsome integerN. It can be implemented with nestedforloops:(a) Make a list of all the integers from 2 throug...

Use phyton

TheSieve of Eratosthonesis an algorithm to find all the prime numbers between 1 andsome integerN. It can be implemented with nestedforloops:(a) Make a list of all the integers from 2 throughN.(b) Cross off all the multiples of 2 (except for 2 itself). The smallest number that remains(after 2) is 3.(c) Cross off all the multiples of 3 (except for 3 itself). The smallest number that remainsis 5.(d) Cross off all the multiples of 5 (except for 5 itself). The smallest number that remainsis 7.(e)···(f) Repeat looking for the smallest numberpthat remains and crossing off all of its multiples(except forpitself) until you reach√N.(g) All of the numbers that remain are primeUse the Sieve to print all the prime numbers under 1000.

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

import math

if __name__=="__main__":
    n=int(input("Enter the value of n: "))
    #creating a list of numbers from 2 to n
    primes=[i for i in range(2,n+1)]
  
    #start with number 2
    p=2
    #when p less than square root of n
    while (p<=math.sqrt(n)):
        #if the value current value in list is not None
        if(primes[p-2]!=None):
            #make all the mulitples as None
            for i in range(p*p,n+1,p):
                primes[i-2]=None
        p=p+1
  
    #print all values in the list which is not prime  
    for i in primes:
        if(i!=None):
            print(i)

############################ PGM END ###################################

OUTPUT
#########

Enter the value of n: 20 2 7 13 17 19

Add a comment
Know the answer?
Add Answer to:
Use phyton TheSieve of Eratosthonesis an algorithm to find all the prime numbers between 1 andsome integerN. It can be implemented with nestedforloops:(a) Make a list of all the integers from 2 throug...
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
  • The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to...

    The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite lie., not prime) the multiples of each prime, starting with the multiples of 2 The sieve of Eratosthenes can be expressed in pseudocode, as follows: Input: an integer n Let A be an array of 8oo1ean values, indexed by integers 2 to n, initially all set to true. for t - 2, 3,...

  • Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the...

    Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...

  • One way to find prime numbers less than n is to treat them as array indices....

    One way to find prime numbers less than n is to treat them as array indices. Mark each position as True initially, assuming that all numbers are prime. Then, starting with index 2, mark all multiples of 2 (greater than 2) as not prime (False). Repeat this for multiples of 3, then multples of 4, etc. When the algorithm terminates, only prime indices will still be True; everything else will be False. The following function takes an integer n as...

  • in visual studio build a masm program that prints out the prime numbers in a array...

    in visual studio build a masm program that prints out the prime numbers in a array L1001-Sieve of Eratosthenes Please use your textbook as a reference. Goal: Use what we have learned to generate prime numbers. Prime numbers have many applications in computer science and as such, efficient ways to discover prime numbers can be very useful. Mathematicians have been intrigued by the concept for ages including the Greek mathematician, Eratosthenes of Cyrene (famous for calculating the circumference o the...

  • The task involves writing a C++ program that determines the prime numbers between 1 and 100....

    The task involves writing a C++ program that determines the prime numbers between 1 and 100. The steps you should follow to identify the prime numbers are the following. 1. The number 1 is not a prime number, so it should be scratched. 2. Starting from the first prime number, which is 2, you scratch all the numbers that are the multiple of 2. You should not scratch out 2 itself. 3. The next number in the sequence after the...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

  • Algorithm Humble Numbers Problem 4-1 (Humble Numbers) 10 points A number k > 1 is called...

    Algorithm Humble Numbers Problem 4-1 (Humble Numbers) 10 points A number k > 1 is called humble if the only prime factors of k are 3 and 5. Consider the task of on input n, outputting the n smallest humble numbers and the following algorithm to do it: HuMBLE(n) count = 0. pret,Output = 0 HEAP.INSERT (3) HEAP.INSERT (5) while (count < n) cur - HEAP. EXTRACTMIN if cur prevOutput then output cur HEAP,INSERT(3*cur) HEAP.İNSERT(5*cur) count -count+1 preu,Output = cur...

  • PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than...

    PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than 1 that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: Create a list with all elements initialized to 1 (true). List elements with prime indexes will remain 1. All other elements will eventually be set to zero. Starting with list element 2, every time a list element is found...

  • 1. Design an algorithm to find all the non-common elements in two sorted lists of numbers....

    1. Design an algorithm to find all the non-common elements in two sorted lists of numbers. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are m and n, ?respectively 2. Estimate how many times faster it will be to find ged(98765, 56789) by Euclid's algorithm compared with the algorithm based on checking consecutive integers from min{m, n} down to gcd(m, n). 3. For each of the following functions, indicate how...

  • 1 For each of the following pairs of numbers a and b, calculate and find integers...

    1 For each of the following pairs of numbers a and b, calculate and find integers r and s such ged (a; b) by Eucledian algorithm that gcd(a; b) = ra + sb. ia= 203, b-91 ii a = 21, b=8 2 Prove that for n 2 1,2+2+2+2* +...+2 -2n+1 -2 3 Prove that Vn 2 1,8" -3 is divisible by 5. 4 Prove that + n(n+1) = nnīYn E N where N is the set of all positive integers....

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
ADVERTISEMENT