forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_maths.py
84 lines (63 loc) · 1.61 KB
/
basic_maths.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
74
75
76
77
78
79
80
81
82
83
84
"""Implementation of Basic Math in Python."""
import math
def prime_factors(n):
"""Find Prime Factors."""
pf = []
while n % 2 == 0:
pf.append(2)
n = int(n / 2)
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
pf.append(i)
n = int(n / i)
if n > 2:
pf.append(n)
return pf
def number_of_divisors(n):
"""Calculate Number of Divisors of an Integer."""
div = 1
temp = 1
while n % 2 == 0:
temp += 1
n = int(n / 2)
div = div * (temp)
for i in range(3, int(math.sqrt(n)) + 1, 2):
temp = 1
while n % i == 0:
temp += 1
n = int(n / i)
div = div * (temp)
return div
def sum_of_divisors(n):
"""Calculate Sum of Divisors."""
s = 1
temp = 1
while n % 2 == 0:
temp += 1
n = int(n / 2)
if temp > 1:
s *= (2**temp - 1) / (2 - 1)
for i in range(3, int(math.sqrt(n)) + 1, 2):
temp = 1
while n % i == 0:
temp += 1
n = int(n / i)
if temp > 1:
s *= (i**temp - 1) / (i - 1)
return s
def euler_phi(n):
"""Calculte Euler's Phi Function."""
l = prime_factors(n)
l = set(l)
s = n
for x in l:
s *= (x - 1) / x
return s
def main():
"""Print the Results of Basic Math Operations."""
print(prime_factors(100))
print(number_of_divisors(100))
print(sum_of_divisors(100))
print(euler_phi(100))
if __name__ == '__main__':
main()