Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kropotov - algo - hw04 #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions hw_04_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from random import randint
import timeit


def f2(a):
min_e = float('inf')
max_e = float('-inf')
for e in a:
min_e = e if e < min_e else min_e
max_e = e if e > max_e else max_e
for idx, e in enumerate(a):
a[idx] = max_e if e == min_e else (min_e if e == max_e else e)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут все же лучше проверять через отдельные условия. Иначе много раз будем перезаписывать одни и те же значения в массив, что не очень хорошо

return a


a100 = [randint(-100, 100) for n1 in range(100)]
a200 = [randint(-200, 200) for n2 in range(200)]
a300 = [randint(-300, 300) for n3 in range(300)]


print(timeit.timeit('f2(a100)', globals=globals())) # 19.445062
print(timeit.timeit('f2(a200)', globals=globals())) # 41.943428600000004
print(timeit.timeit('f2(a300)', globals=globals())) # 63.8597697

# можно сделать вывод что сложность алгоритма O(n), т.е. пропорциональна размеру входного массива

77 changes: 77 additions & 0 deletions hw_04_02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import math
import timeit


def find_prime(pos):
ind = 1
simples = [2]
cur = 3
while ind < pos:
rest = 0
for d in simples:
rest = cur % d
if rest == 0:
break
if rest != 0:
simples.append(cur)
ind += 1
cur += 2
return simples[len(simples) - 1]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return simples[-1]



print(find_prime(10))
print(timeit.timeit('find_prime(10)', globals=globals(), number=100000))

print(find_prime(20))
print(timeit.timeit('find_prime(20)', globals=globals(), number=100000))

print(find_prime(40))
print(timeit.timeit('find_prime(40)', globals=globals(), number=100000))


def find_prime_re(pos):
lim = math.ceil(2 * pos * math.log(pos))
ma = list(range(2, lim))
start_ind = 0
cur_simple = 0
res = 0
while start_ind < len(ma):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len(ma) у нас постоянное значение, поэтому лучше один раз вычислить его и хранить в переменной отдельной. так быстрее будет работать программа

if ma[start_ind] != 0:
cur_simple += 1
if cur_simple == pos:
res = ma[start_ind]
break
step = ma[start_ind]
cur_pos = start_ind + step
while cur_pos < len(ma):
ma[cur_pos] = 0
cur_pos += step
start_ind += 1
return res


print(find_prime_re(10))
print(timeit.timeit('find_prime_re(10)', globals=globals(), number=100000))
print(find_prime_re(20))
print(timeit.timeit('find_prime_re(20)', globals=globals(), number=100000))
print(find_prime_re(40))
print(timeit.timeit('find_prime_re(40)', globals=globals(), number=100000))

# 29
# 0.6340101
# 71
# 1.9487579999999998
# 173
# 6.395146
# 29
# 1.2755858
# 71
# 3.2944619
# 173
# 11.313060799999999

# Видимо не правильно реализовал второй алгоритм, хотя делал как понял.
# Нужно было определить верхню границу для i-го простого числа, нашел формулу 2*n*ln(n)
Comment on lines +73 to +74
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

реализация правильная (в алгоритмическом смысле). Просто не самый эффективный код написан, ничего страшного в этом нету :)