Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions memory_profiler_task_01_1.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Filename: /home/krugloff/PycharmProjects/algorithms/lesson_060/task_01_1.py

Line # Mem usage Increment Line Contents
================================================
8 14.394531 MiB 14.394531 MiB @profile(precision=6, stream=fp)
9 def eratosthenes(n):
10 14.394531 MiB 0.000000 MiB a = [0] * n # создание массива с n количеством элементов
11 14.750000 MiB 0.000000 MiB for i in range(n): # заполнение массива ...
12 14.750000 MiB 0.355469 MiB a[i] = i # значениями от 0 до n-1
13
14 # вторым элементом является единица, которую не считают простым числом
15 # забиваем ее нулем.
16 14.750000 MiB 0.000000 MiB a[1] = 0
17
18 14.750000 MiB 0.000000 MiB m = 2 # замена на 0 начинается с 3-го элемента (первые два уже нули)
19 14.750000 MiB 0.000000 MiB while m < n: # перебор всех элементов до заданного числа
20 14.750000 MiB 0.000000 MiB if a[m] != 0: # если он не равен нулю, то
21 14.750000 MiB 0.000000 MiB j = m * 2 # увеличить в два раза (текущий элемент простое число)
22 14.750000 MiB 0.000000 MiB while j < n:
23 14.750000 MiB 0.000000 MiB a[j] = 0 # заменить на 0
24 14.750000 MiB 0.000000 MiB j = j + m # перейти в позицию на m больше
25 14.750000 MiB 0.000000 MiB m += 1
26
27 # вывод простых чисел на экран (может быть реализован как угодно)
28 14.750000 MiB 0.000000 MiB b = []
29 14.750000 MiB 0.000000 MiB for i in a:
30 14.750000 MiB 0.000000 MiB if a[i] != 0:
31 14.750000 MiB 0.000000 MiB b.append(a[i])
32
33 14.750000 MiB 0.000000 MiB del a
34 14.750000 MiB 0.000000 MiB return b


76 changes: 76 additions & 0 deletions memory_profiler_task_01_2.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
Filename: /home/krugloff/PycharmProjects/algorithms/lesson_060/task_01_2.py

Line # Mem usage Increment Line Contents
================================================
9 14.394531 MiB 14.394531 MiB @profile(precision=6, stream=fp)
10 def atkin(nmax):
11 """
12 Returns a list of prime numbers below the number "nmax"
13 """
14 14.394531 MiB 0.000000 MiB is_prime = dict([(i, False) for i in range(5, nmax + 1)])
15 14.394531 MiB 0.000000 MiB for x in range(1, int(math.sqrt(nmax)) + 1):
16 14.394531 MiB 0.000000 MiB for y in range(1, int(math.sqrt(nmax)) + 1):
17 14.394531 MiB 0.000000 MiB n = 4 * x ** 2 + y ** 2
18 14.394531 MiB 0.000000 MiB if (n <= nmax) and ((n % 12 == 1) or (n % 12 == 5)):
19 14.394531 MiB 0.000000 MiB is_prime[n] = not is_prime[n]
20 14.394531 MiB 0.000000 MiB n = 3 * x ** 2 + y ** 2
21 14.394531 MiB 0.000000 MiB if (n <= nmax) and (n % 12 == 7):
22 14.394531 MiB 0.000000 MiB is_prime[n] = not is_prime[n]
23 14.394531 MiB 0.000000 MiB n = 3 * x ** 2 - y ** 2
24 14.394531 MiB 0.000000 MiB if (x > y) and (n <= nmax) and (n % 12 == 11):
25 14.394531 MiB 0.000000 MiB is_prime[n] = not is_prime[n]
26 14.394531 MiB 0.000000 MiB for n in range(5, int(math.sqrt(nmax)) + 1):
27 14.394531 MiB 0.000000 MiB if is_prime[n]:
28 14.394531 MiB 0.000000 MiB ik = 1
29 14.394531 MiB 0.000000 MiB while (ik * n ** 2 <= nmax):
30 14.394531 MiB 0.000000 MiB is_prime[ik * n ** 2] = False
31 14.394531 MiB 0.000000 MiB ik += 1
32 14.394531 MiB 0.000000 MiB primes = []
33 14.394531 MiB 0.000000 MiB for i in range(nmax + 1):
34 14.394531 MiB 0.000000 MiB if i in [0, 1, 4]:
35 14.394531 MiB 0.000000 MiB pass
36 14.394531 MiB 0.000000 MiB elif i in [2, 3] or is_prime[i]:
37 14.394531 MiB 0.000000 MiB primes.append(i)
38 else:
39 14.394531 MiB 0.000000 MiB pass
40 14.394531 MiB 0.000000 MiB return primes


Filename: /home/krugloff/PycharmProjects/algorithms/lesson_060/task_01_2.py

Line # Mem usage Increment Line Contents
================================================
9 14.750000 MiB 14.750000 MiB @profile(precision=6, stream=fp)
10 def atkin(nmax):
11 """
12 Returns a list of prime numbers below the number "nmax"
13 """
14 15.894531 MiB 1.144531 MiB is_prime = dict([(i, False) for i in range(5, nmax + 1)])
15 15.894531 MiB 0.000000 MiB for x in range(1, int(math.sqrt(nmax)) + 1):
16 15.894531 MiB 0.000000 MiB for y in range(1, int(math.sqrt(nmax)) + 1):
17 15.894531 MiB 0.000000 MiB n = 4 * x ** 2 + y ** 2
18 15.894531 MiB 0.000000 MiB if (n <= nmax) and ((n % 12 == 1) or (n % 12 == 5)):
19 15.894531 MiB 0.000000 MiB is_prime[n] = not is_prime[n]
20 15.894531 MiB 0.000000 MiB n = 3 * x ** 2 + y ** 2
21 15.894531 MiB 0.000000 MiB if (n <= nmax) and (n % 12 == 7):
22 15.894531 MiB 0.000000 MiB is_prime[n] = not is_prime[n]
23 15.894531 MiB 0.000000 MiB n = 3 * x ** 2 - y ** 2
24 15.894531 MiB 0.000000 MiB if (x > y) and (n <= nmax) and (n % 12 == 11):
25 15.894531 MiB 0.000000 MiB is_prime[n] = not is_prime[n]
26 15.894531 MiB 0.000000 MiB for n in range(5, int(math.sqrt(nmax)) + 1):
27 15.894531 MiB 0.000000 MiB if is_prime[n]:
28 15.894531 MiB 0.000000 MiB ik = 1
29 15.894531 MiB 0.000000 MiB while (ik * n ** 2 <= nmax):
30 15.894531 MiB 0.000000 MiB is_prime[ik * n ** 2] = False
31 15.894531 MiB 0.000000 MiB ik += 1
32 15.894531 MiB 0.000000 MiB primes = []
33 15.894531 MiB 0.000000 MiB for i in range(nmax + 1):
34 15.894531 MiB 0.000000 MiB if i in [0, 1, 4]:
35 15.894531 MiB 0.000000 MiB pass
36 15.894531 MiB 0.000000 MiB elif i in [2, 3] or is_prime[i]:
37 15.894531 MiB 0.000000 MiB primes.append(i)
38 else:
39 15.894531 MiB 0.000000 MiB pass
40 15.894531 MiB 0.000000 MiB return primes


Loading