-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_algorithm_book.py
More file actions
55 lines (40 loc) · 1022 Bytes
/
Copy pathexample_algorithm_book.py
File metadata and controls
55 lines (40 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import time
import math
def summa(lst):
if lst == []:
return 0
return (lst[0] + summa(lst[1:]))
def count(lst):
if lst == []:
return 0
return (1 + count(lst[1:]))
def check_collatz():
def collatz(number):
if number%2 == 0:
return number//2
elif number%2 == 1:
return 3*number+1
def check_input():
i = None
while i != int():
try:
i = int(input())
return i
except ValueError as err:
print(err)
i = check_input()
start_time = time.perf_counter()
while i > 1:
i = collatz(i)
print(i)
print ("{:g} s".format(time.perf_counter() - start_time))
def check_fibonachchi():
def fib(n,k):
return (math.factorial(n)/(math.factorial(k)*(math.factorial(n-k))))
n, k = map(int, input().split())
if k == 0:
print('1')
elif k > n:
print('0')
else:
print(int(fib(n,k)))