🔢Algoritma Örnekleri
🎈 Asal Sayı Hesaplama (Prime)
The first optimization takes advantage of the fact that two is the only even prime. Thus we can check if a number is even and as long as its greater than 2, we know that it is not prime.
🔢 Mersenne Number
🪁 Lucas Lehmer
🔢 Mersenne Prime
💯 Sieve of Eratosthenes
The method works as follows (see here for more details)
Generate a list of all numbers between 0 and N; mark the numbers 0 and 1 to be not prime
Starting with $p=2$ (the first prime) mark all numbers of the form $np$ where $n>1$ and $np <= N$ to be not prime (they can't be prime since they are multiples of 2!)
Find the smallest number greater than $p$ which is not marked and set that equal to $p$, then go back to step 2. Stop if there is no unmarked number greater than $p$ and less than $N+1$
We will break this up into a few functions, our general strategy will be to use a Python list
as our container although we could use other data structures. The index of this list will represent numbers.
We have implemented a sieve
function which will find all the prime numbers up to $n$. You will need to implement the functions which it calls. They are as follows
list_true
Make a list of true values of length $n+1$ where the first two values are false (this corresponds with step 1 of the algorithm above)mark_false
takes a list of booleans and a number $p$. Mark all elements $2p,3p,...n$ false (this corresponds with step 2 of the algorithm above)find_next
Find the smallestTrue
element in a list which is greater than some $p$ (has index greater than $p$ (this corresponds with step 3 of the algorithm above)prime_from_list
Return indices of True values
Remember that python lists are zero indexed. We have provided assertions below to help you assess whether your functions are functioning properly.
👨💻 Code Snippets
📂 Yüksek Miktarda Veriyi Ele Alma
Last updated