🔢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.
Our second optimization takes advantage of the fact that when checking factors, we only need to check odd factors up to the square root of a number. Consider a number decomposed into factors . There are two cases, either is prime and without loss of generality, or is not prime and . In this case, if , then . So we only need to check all possible values of and we get the values of for free! This means that even the simple method of checking factors will increase in complexity as a square root compared to the size of the number instead of linearly.
🔢 Mersenne Number
A Mersenne number is any number that can be written as for some . For example, 3 is a Mersenne number ( ) as is 31 ( ). We will see later on that it is easy to test if Mersenne numbers are prime.
🪁 Lucas Lehmer
We can test if a Mersenne number is prime using the Lucas-Lehmer test. First let's write a function that generates the sequence used in the test. Given a Mersenne number with exponent , the sequence can be defined as
🔢 Mersenne Prime
For a given Mersenne number with exponent , the number is prime if the Lucas-Lehmer series is 0 at position . Write a function that tests if a Mersenne number with exponent is prime. Test if the Mersenne numbers with prime between 3 and 65 (i.e. 3, 5, 7, ..., 61) are prime. Your final answer should be a list of tuples consisting of (Mersenne exponent, 0)
(or 1
) for each Mersenne number you test, where 0
and 1
are replacements for False
and True
respectively.
💯 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