Advertisement
1nikitas

Sieve of Eratosthenes

Jun 29th, 2020
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. #   Hello, today i want to tell you some informantion about prime numbers
  2. #   and algoritms, which help to work with faster and easier.
  3. #   Prime number is number, whose divisors are 1 and the number itself.
  4.  
  5. #   So, lets code a function, which will return all prime numbers in range from 1 to N.
  6.  
  7. def isPrime(n):
  8.   allPrimes = []
  9.  
  10.   for k in range(2, n+1):
  11.     prime = True
  12.     for i in range(2, k):
  13.         if k%i == 0:
  14.             prime = False
  15.             break
  16.            
  17.     if prime:
  18.         allPrimes.append(k)
  19.   return allPrimes
  20.  
  21. #   The asymptotic complexity of this algorithm is O(N^2)
  22.  
  23. #   The asymptotic complexity of an algorithm is a method for estimating the computational complexity
  24. #   of an algorithm "up to a constant", used in complexity theory.
  25.  
  26.  
  27.  
  28. #   This algorithm is very slow. Why? Because when N will be more then 10^6 it will work for 10-15 secs.
  29. #   We dont have this time, thats why I would like to show you The Eieve of Eratosthen.
  30.  
  31. #   The idea of this algo is very easy:
  32. #   We will write down a series of numbers 1...n, and we will first cross out all the numbers divisible by 2,
  33. #   except for the number 2 itself, then divisible by 3, except for the number 3 itself, then by 5,
  34. #   then by 7, 11, and all the other primes up to n.
  35.  
  36. #   So, lets code =)
  37.  
  38. def eratosthenes(N):
  39.     a = [i for i in range(1, N+1)] #    there would be prime numbes
  40.     b = [] #    there would be crossed
  41.     num=2   #   prime numbers, which will be checkes, so 2, 3, 5...
  42.     i=0 #   counter
  43.     a.remove(1) #   1 - not prime numbee
  44.    
  45.     while num < a[-1]: #    while num is less than the last number in list a
  46.         while len(a) > i:   #   while as long as the length of a is greater than i, that is, it is a
  47.                             # loop that goes through the list looking for numbers that are multiples of num and deletes them
  48.             if a[i]%num == 0 and a[i]!= num:    # if the remainder of the number in the list at position i is zero ,
  49.                                                 # we add it to the deleted list(b) and delete it from a
  50.                 b.append(a[i]) #    adding
  51.                 a.pop(i) #  deleting
  52.             i+=1 #  check the next position
  53.         i=0 #   resetting the counter
  54.  
  55.         # next num
  56.         for j in range(len(a)):
  57.             if a[j] > num:
  58.                 num = a[j]
  59.                 break
  60.  
  61.     return b
  62.  
  63. #   The asymptotic complexity of this algorithm is O(N*log(log(n))), So you can find a prime fast =)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement