Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana committed Jan 20, 2019
1 parent 4f43947 commit 890447d
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 0 deletions.
109 changes: 109 additions & 0 deletions task_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# 1. Проанализировать скорость и сложность одного любого алгоритма, разработанных в рамках домашнего задания первых трех уроков.
#
# В диапазоне натуральных чисел от 2 до 99 определить, сколько из них кратны любому из чисел в диапазоне от 2 до 9.
import cProfile

RANGE_START = 2


# Способ 1. Перебор
def func1(range_stop):
nums_data = dict()
for i in range(2, 10):
nums_data[i] = 0

for num in range(RANGE_START, range_stop + 1):
for i in nums_data.keys():
if num % i == 0:
nums_data[i] += 1

return nums_data


# 100 loops, best of 5: 53.6 usec per loop - 99
# 100 loops, best of 5: 551 usec per loop - 999
# 100 loops, best of 5: 5.75 msec per loop - 9999

# cProfile.run('func1(99)')
#
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 0.000 0.000 <string>:1(<module>)
# 1 0.000 0.000 0.000 0.000 task_1.py:7(func1)
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
# 98 0.000 0.000 0.000 0.000 {method 'keys' of 'dict' objects}

# cProfile.run('func1(999)')
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 0.001 0.001 <string>:1(<module>)
# 1 0.001 0.001 0.001 0.001 task_1.py:7(func1)
# 1 0.000 0.000 0.001 0.001 {built-in method builtins.exec}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
# 998 0.000 0.000 0.000 0.000 {method 'keys' of 'dict' objects}

# print(func1(99))

# Способ 2. Целочисленное деление
def func2(range_stop):
nums_data = dict()
for i in range(2, 10):
nums_data[i] = range_stop // i

return nums_data


# 100 loops, best of 5: 901 nsec per loop - 99
# 100 loops, best of 5: 950 nsec per loop - 999
# 100 loops, best of 5: 965 nsec per loop - 9999
# 100 loops, best of 5: 966 nsec per loop - 99999

# cProfile.run('func2(99)')
#
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 0.000 0.000 <string>:1(<module>)
# 1 0.000 0.000 0.000 0.000 task_1.py:45(func2)
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

# cProfile.run('func2(999)')
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 0.000 0.000 <string>:1(<module>)
# 1 0.000 0.000 0.000 0.000 task_1.py:45(func2)
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}


# Способ 3. Пройти диапазон для каждого числа 2-9
def func3(range_stop):
nums_data = dict()
for i in range(2, 10):
nn = 0
for num in range(RANGE_START, range_stop + 1):
if num % i == 0:
nn += 1
nums_data[i] = nn

return nums_data

#100 loops, best of 5: 34.8 usec per loop - 99
#100 loops, best of 5: 397 usec per loop - 999
#100 loops, best of 5: 4.27 msec per loop - 9999

# cProfile.run('func3(99)')
#
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 0.000 0.000 <string>:1(<module>)
# 1 0.000 0.000 0.000 0.000 task_1.py:77(func3)
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}


# print(func1(99))
# print(func2(99))
# print(func3(99))


# Вывод
# Ранее задача была решена самым ресурсоемким способом. Способ 3 быстрее способа 1 за счет отсутствия обращения к ключам словаря.
# Самый быстрый способ - Способ 2.

122 changes: 122 additions & 0 deletions task_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# 2. Написать два алгоритма нахождения i-го по счёту простого числа.


import cProfile

# Без использования «Решета Эратосфена»;

def func1(n):
snums = [2]
last_num = 3
while len(snums) < n:
for snum in snums:
if last_num % snum == 0:
break
else:
snums.append(last_num)

last_num += 1
return snums[len(snums) - 1]


# 100 loops, best of 5: 6.7 usec per loop - 10
# 100 loops, best of 5: 19.1 usec per loop - 20
# 100 loops, best of 5: 273 usec per loop - 100

# cProfile.run('func1(10)')

# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 0.000 0.000 <string>:1(<module>)
# 1 0.000 0.000 0.000 0.000 task_2.py:7(func1)
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
# 29 0.000 0.000 0.000 0.000 {built-in method builtins.len}
# 9 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

# cProfile.run('func1(1000)')
#
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 0.027 0.027 <string>:1(<module>)
# 1 0.026 0.026 0.027 0.027 task_2.py:7(func1)
# 1 0.000 0.000 0.027 0.027 {built-in method builtins.exec}
# 7919 0.001 0.000 0.001 0.000 {built-in method builtins.len}
# 999 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

# print(func1(10))

# Используя алгоритм «Решето Эратосфена»

def func2(n):
pi_func = {
4: 10,
25: 10 ** 2,
168: 10 ** 3,
1229: 10 ** 4,
9592: 10 ** 5,
78498: 10 ** 6,
664579: 10 ** 7,
5761455: 10 ** 8
}

for num in pi_func.keys():
if n <= num:
size = pi_func[num]
break
else:
return 'Ошибка! Сликом большое число'

a = [0] * size
for i in range(size):
a[i] = i

a[1] = 0

m = 2
nnum = 0
while m < size:
if a[m] != 0:

nnum += 1
if nnum == n:
return m

j = m * 2
while j < size:
a[j] = 0
j = j + m

m += 1

# 100 loops, best of 5: 13.4 usec per loop - 10
# 100 loops, best of 5: 17.4 usec per loop - 20
# 100 loops, best of 5: 204 usec per loop - 100


# cProfile.run('func2(10)')
#
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 0.000 0.000 <string>:1(<module>)
# 1 0.000 0.000 0.000 0.000 task_2.py:49(func2)
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
# 1 0.000 0.000 0.000 0.000 {method 'keys' of 'dict' objects}


# cProfile.run('func2(1000)')

# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 0.003 0.003 <string>:1(<module>)
# 1 0.003 0.003 0.003 0.003 task_2.py:49(func2)
# 1 0.000 0.000 0.003 0.003 {built-in method builtins.exec}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
# 1 0.000 0.000 0.000 0.000 {method 'keys' of 'dict' objects}


#Вывод:
#1. Использование решета Эратосфена более долгий метод на маленьких значениях n, но существенно более выигрышний с ростом n
#2. Функцию func1 можно облегчить за счет отказа от использования метода len - хранить длину в отдельной переменной,
# но в целом это сильно скажется на производительности алгоритма

# print(func1(1000))
# print(func2(1000))

0 comments on commit 890447d

Please sign in to comment.