Note that there are some explanatory texts on larger screens.

plurals
  1. POpython coding - Sieve of eratosthenes how to draw from list?
    primarykey
    data
    text
    <p>Hi all I have a question im working on in python and seem to be stuck on step 3 and 4. Any help would be great. This is the question:</p> <p>Write a program which implements a function for the sieve of Eratosthenes. (<a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes</a>). The sieve of Eratosthenes is a simple algorithm for finding all prime numbers up to a specified integer. It was created by the ancient Greek mathematician Eratosthenes. The algorithm to find all the prime numbers less than or equal to a given integer n:</p> <ol> <li>Create a list of integers from two to n: 2, 3, 4, ..., n</li> <li>Start with a counter i set to 2, i.e. the first prime number</li> <li>Starting from i+i, count up by i and remove those numbers from the list, i.e. 2*i, 3*i, 4*i, and so on...</li> <li>Find the first number of the list following i. This is the next prime number.</li> <li>Set i to the number found in the previous step</li> <li>Repeat steps 3 and 4 until i is greater than n.</li> <li>All the numbers, which are still in the list, are prime numbers</li> </ol> <p>This is what I have coded so far just don't know how to get the prime numbers from the list and remove the others....:</p> <pre><code>def main(): n = int(input("Enter a limiting number to find the prime numbers smaller than it: ")) num_list = [] num = 1 for i in range(2, n + 1): num = num + 1 num_list.append(num) i = 2 for p in range(2, n): non_prime = num * i #while non_prime &lt; n: if non_prime == num: num_list.remove(num) i = i + 1 print(num_list) main() # Call the main function </code></pre> <p>Thank for your help im banging my head against the wall as we speak.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload