Question

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 whose value is 1, loop through the remainder of the list and set to zero every element whose index is a multiple of the index for the element with value 1. For list index 2, all elements beyond 2 in the list that are multiples of 2 will be set to zero (indexes 4, 6, 8, 10, etc.); for list index 3, all elements beyond 3 in the list that are multiples of 3 will be set to zero (indexes 6, 9, 12, 15, etc.); and so on.

When this process is complete, the list elements that are still set to 1 indicate that the index is a prime number. These indexes can then be printed. Write a program that uses a list of 1000 elements to determine and print the prime numbers between 2 and 999. Ignore element 0 of the list. The prime numbers must be printed out 10 numbers per line.

I use the def function, but i cant figure out x%multipliers ==0 in a loop. Sample execution: Python 3.64 Shell File Edit Shell Debug Options Window Help Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel) on win32 Type copyright, credits or license () for more information RESTART: P:\Cmpsc131\Programming Assignment Solutions \EC2PrimeNumberGenerator.py Prime numbers between 2 and 999 as determined by the Sieve of Eratosthenes 2 3 5 7 11 13 17 19 23 29 31 3741 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367373 379 383 389 397 401 409 419 421 431433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967971 977 983 991 997

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

######################## PGM START #############################################

def Sieve_Of_Eratosthenes(limit):
  
    # Create an array "priArray[0..n]" and initialize
    # all entries it as 1. A value in p[i] will
    # finally be 0 if i is not a prime, else the
    # value will be true.
   priArray=[];
    for i in range(limit+1):
        priArray.append(1);

  
    p = 2;
    while (p * p <= limit):

        # If priArray[p] is not changed, then it is a prime
        if (priArray[p] == 1):
            # Update all multiples of p
            i=p*2;
            while(i<=limit):
                priArray[i] = 0;
                i=i+p;
        p += 1

    
    # Print all prime numbers
   count=0
    for i in range(2, limit):

        if priArray[i]:
            print(i,end=" ")
            count+=1
        if(count==10):
            print("")
            count=0


# driver program
if __name__=='__main__':
    upper_Limit = 1000;
    print ("Prime number between 2 and 999 as determined by Sieve Of Eratosthenes")
    Sieve_Of_Eratosthenes(upper_Limit)

######################### PGM END ########################################
OUTPUT
########

Prime number between 2 and 999 as determined by Sieve Of Eratosthenes 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

Add a comment
Know the answer?
Add Answer to:
PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than...
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,...

  • Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate...

    Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate the diameter of the earth. For several decades he served as the director and chief librarian of the famous library in Alexandria. He was highly regarded in the ancient world, but unfortunately only fragments of his writing have survived. The algorithm described for this assignment is known as the Sieve of Eratosthenes. The algorithm is designed to find all prime numbers within a given...

  • For Python: A prime number is defined as an integer greater than 1 that has no...

    For Python: A prime number is defined as an integer greater than 1 that has no positive divisors other than 1 and itself. Write a program that prompts the user for an integer > 1. Validate the value is > 1 (if not, ask for another). Use a loop to determine if the number is prime or not. Issue an appropriate message. [complete this part before proceeding]. Add a loop that continues to ask the user if they would like...

  • 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...

  • A Prime Number is an integer which is greater than one, and whose only factors are...

    A Prime Number is an integer which is greater than one, and whose only factors are 1 and itself. Numbers that have more than two factors are composite numbers. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. The number 1 is not a prime number. Write a well-documented, Python program - the main program, that accepts both a lower and upper integer from user entries. Construct a function, isPrime(n), that takes...

  • A positive integer greater than 1 is said to be prime if it has no divisors...

    A positive integer greater than 1 is said to be prime if it has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime. Write a program that defines two functions is_prime() and list_primes(). is_prime() will determine if a number is prime. list_primes() will list all the prime numbers below itself other than 1. For example, the first five prime numbers are: 2, 3, 5, 7 and 11." THE PROGRAM...

  • 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...

  • #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements....

    #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...

  • 4.3 Ch 4 Ex 14 - sieve of Erotosthenes In this lab, you will write a...

    4.3 Ch 4 Ex 14 - sieve of Erotosthenes In this lab, you will write a program to compute all the prime numbers between 2 and some other integer, using an algorithm called the Sieve of Erotosthenes. First you will read, from standard input, the integer that will be the upper limit of the range of primes. Call it max. Then, create a vector<int> with max elements, and call it is_composite. The result of the Sieve will be that is_composite[i]...

  • 4.3 Ch 4 Ex 14 - sieve of Erotosthenes In this lab, you will write a...

    4.3 Ch 4 Ex 14 - sieve of Erotosthenes In this lab, you will write a program to compute all the prime numbers between 2 and some other integer, using an algorithm called the Sieve of Erotosthenes. First you will read, from standard input, the integer that will be the upper limit of the range of primes. Call it max. Then, create a vector<int> with max elements, and call it is_composite. The result of the Sieve will be that is_composite[i]...

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