-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_theory.py
117 lines (102 loc) · 1.98 KB
/
number_theory.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import primes
def tau(n):
'''the number-theoretic method for computing the number of factors of n'''
p = primes.pf(n)
product = 1
for i in range(len(p)):
product*= (p[i][1]+1)
return product
def sigma(n):
'''the number-theoretic method for computing the sum of the factors of n'''
if n == 0:
return 0
else:
product = 1
p = primes.pf(n)
for i in range(len(p)):
product *= ((p[i][0]**(p[i][1]+1))-1)/(p[i][0]-1)
return product
def nu(n):
'''sigma/n.'''
return sigma(n)/float(n)
def isab(n):
'''test whether or not a number is abundant'''
if nu(n)>2:
return True
else:
return False
def isper(n):
'''test whether n is perfect'''
if nu(n)==2:
return True
else:
return False
def isdef(n):
'''test whether n is deficient'''
if nu(n)<2:
return True
else:
return False
def isprimab(n):
'''test whether n is primitive abundant'''
if isper(n):
return True
elif isab(n):
f = primes.propdiv(n)
x = True
for i in range(len(f)):
if x:
x = x and isdef(f[i])
else:
break
return x
else:
return False
def phi(n):
'''The Euler phi function'''
p = primes.pf(n)
for i in range(len(p)):
n=(n/p[i][0])*(p[i][0]-1)
return n
def GCD(a,b):
'''Euclidean Algorithm method of computing greatest common divisor'''
while not b == 0:
r = a%b
a = b
b = r
return a
def LCM(a,b):
'''returns least common multiple'''
return a*b/GCD(a,b)
def mu(n):
'''mu inversion function'''
p = primes.pf(n)
i = 0
t = 1
while i < len(p):
if p[i][1] == 1:
t*=(-1)
i+= 1
else:
t = 0
break
return t
def sumfactorbuilder(fn):
''' returns a function that is the sum of the values\n
of the original function of all the divisors of n'''
def F(n):
f = primes.factors(n)
s = 0
for i in range(len(f)):
s += fn(f[i])
return s
return F
def muinversebuilder(fn):
"""reverses sumfactorbuilder's operation."""
def F(n):
f = primes.factors(n)
s = 0
for i in range(len(f)):
s+= (mu(f[i])*fn(n/f[i]))
return s
return F