🔢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 nn decomposed into factors n=abn=ab . There are two cases, either nn is prime and without loss of generality, a=n,b=1a=n, b=1 or nn is not prime and a,bn,1a,b \neq n,1 . In this case, if a>na > \sqrt{n} , then b<nb<\sqrt{n} . So we only need to check all possible values of bb and we get the values of aa 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 2p12^p - 1 for some pp . For example, 3 is a Mersenne number ( 2212^2 - 1 ) as is 31 ( 2512^5 - 1 ). 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 pp , the sequence can be defined as

n0=4n_0 = 4 ni=(ni122)mod(2p1)n_i = (n_{i-1}^2 - 2) mod (2^p - 1)

🔢 Mersenne Prime

For a given Mersenne number with exponent pp , the number is prime if the Lucas-Lehmer series is 0 at position p2p-2 . Write a function that tests if a Mersenne number with exponent pp is prime. Test if the Mersenne numbers with prime pp 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)

  1. Generate a list of all numbers between 0 and N; mark the numbers 0 and 1 to be not prime

  2. 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!)

  3. 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 smallest True 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

[x for x in iter if x = 1]
sum([obj["items"] for obj in groups[name]]
[(name, sum([obj["items"] for obj in groups[name]])) for name in groups]
lis = [("a",1) ...]
max(lis, key=lambda x:x[1]) # 2. elemana göre hesaplama

📂 Yüksek Miktarda Veriyi Ele Alma

{
    field: {
        name : [ # Be careful it's list (not dict)
            data: {
                "items": 5 # some value
            },
            ...
        ],
        ...
    },
    ...
}

Last updated

© 2024 ~ Yunus Emre Ak ~ yEmreAk