-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathfuncs.py
73 lines (56 loc) · 1.4 KB
/
mathfuncs.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import math
from decimal import Decimal, getcontext
def absolute(n):
"""
Returns the Absolute value of a number
"""
return abs(n)
def negitive(n):
"""
Returns if a number is negitive (True) or Positive (False)
"""
return abs(n) != n
def power_of_2(n):
"""
Returns if the number is a power of 2, in which case it will return the log2 of it, else, it'll return false
"""
if (math.log(n)/math.log(2)).is_integer():
# Is a power of 2
return math.log2(n)
else:
#Not a power
return False
def factors(n):
"""
Finds all factors of a number and returns as a list
"""
factors = []
for factor in range(1, n + 1):
if n % factor == 0:
factors.append(factor)
return factors
def multipliers(n):
"""
Takes th facotrs and turns them into a x*y format:
e.g input 8 output: {8:1, 4:2, 2:4, 1:8}
"""
factor = factors(n)
multiples = {}
for i in range(len(factor)):
multiples[factor[i]] = factor[len(factor) - 1 - i]
return multiples
def power_of_3(n):
"""
Returns if the number is a power of 3, in which case it will return the log3 of it, else, it'll return false
"""
j = 1
i = 0
while i < 10001:
j *= 3
i += 1
if j == n:
return i
else:
pass
else:
return False