Question

In: Computer Science

IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.

IN PYTHON

Use the sieve of Eratosthenes to find all primes less than 10,000.

Solutions

Expert Solution

# all primes less than 10,000

n = 10000

# initialize array of n elements to true

primes = [True for i in range(n)]

# set 0 and 1 as false

primes[0]=primes[1]=False

# starting from first prime

current = 2

while (current**2 <= n):

    # if current number is prime

    if (primes[current] == True):

        # set all multiples of current to false

        for i in range(current**2, n, current):

            primes[i] = False

    # increment current

    current += 1

print("Primes less than 10,000 are:")

# print numbers in primes array which are true

for prime in range(n):

    if primes[prime]:

        print(prime,end=' ')

print()

.

Screenshot:

.

Output:

.


Related Solutions

Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given...
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given limit. Aim Develop code for implementing the sieve of Eratosthenes. Steps for Completion Implement the isPrime() method of the SieveOfEratosthenes class that should return true if the number is prime, and false otherwise. Consider building the sieve in the class constructor. CODE GIVEN public class SieveOfEratosthenes { public SieveOfEratosthenes(int maxValue) { // Build the sieve here } public boolean isPrime(int value) { // Write...
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,” Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n - 1, whether the number is prime. In Java
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes....
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes. Example Program Session (implement some linefeed ‘\n’ formatting): Enter the limit: 1000 Primes up to 1000    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...
Find all primes p for which −5 is a quadratic residue.
Find all primes p for which −5 is a quadratic residue.
Given a number n, what is the largest gap between successive primes which are less than...
Given a number n, what is the largest gap between successive primes which are less than number n? For example, if n = 12, the prime numbers less than 12 are: [2, 3, 5, 7, 11]. The largest gap between any two prime numbers in this list is 4, so the function would return '4'. >>>print(largestGap(12)) 4 Write Python code to solve this problem, and include the following 3 test cases in your answer. >>>print(largestGap(100)) 8 >>>print(largestGap(1000)) 20 >>>print(largestGap(10000)) 36...
Consider all positive integers less than 100. Find the number of integers divisible by 3 or...
Consider all positive integers less than 100. Find the number of integers divisible by 3 or 5? Consider strings formed from the 26 English letters. How many strings are there of length 5? How many ways are there to arrange the letters `a',`b', `c', `d', and `e' such that `a' is not immediately followed by`e' (no repeats since it is an arrangement)?
find all primes p such that 6 is a square mod p? please with clear hand...
find all primes p such that 6 is a square mod p? please with clear hand writing
Use "do...while" loops to write a program that finds all prime numbers less than a specified...
Use "do...while" loops to write a program that finds all prime numbers less than a specified value.
How to use python to find all longest common subsequences of 2 sequences? In addition, please...
How to use python to find all longest common subsequences of 2 sequences? In addition, please also analyze the space complexity. For example, the input is "ABCBDAB" and "BDCABA", then the lcs should be "BCAB" "BDAB" and "BCBA".
In Python Suppose there is a movie theater who charges ticket based on ages. Less than...
In Python Suppose there is a movie theater who charges ticket based on ages. Less than 3 years old, free; between 3 to 12, $10; more than 12 years old, $15. Please design a program with loop to, 1) Ask the name then age of the audience, 2) Based on audience's input, tell the audience (with the person's name) how much is the ticket, 3) Stop the program when type in 'quit' in your program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT