-
Notifications
You must be signed in to change notification settings - Fork 0
/
other_functions.py
49 lines (41 loc) · 1.31 KB
/
other_functions.py
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
def notfactors(list1: list, n: int) -> bool:
"""
Args :
list1: list - list of int
n: int - n >= 1
Returns :
bool - True if no number in list1 is factor of n. In other case returns False.
"""
list1 = sorted(list1)
for i in list1:
if n % i == 0 and n > i:
return False
if n <= i:
return True
return True
def notfactors_forprime(list1: list, n: int) -> bool:
# Variation of notfactors function for more efficiency
# Useful to determine if n is prime if the numbers of list1 are primes and < n.
"""
Args :
list1: list - list of int
n: int - n >= 1
Returns :
bool - True if no number in list1 is factor of n. In other case returns False.
"""
# list = sorted(list) # necessary only when list is not sorted
for i in list1[:int((len(list1)+1)**(1/2))+1]:
if n % i == 0:
return False
return True
def factorsproduct(list1: list) -> float:
# Returns the product of all elements of a list in a recursive way
"""
Args :
list1: list - list of int
Returns :
float - product of all elements of list1
"""
if len(list1) == 1:
return float(list1[0])
return float(list1[len(list1)-1])*factorsproduct(list1[:len(list1)-1])