2019-07-02 04:05:43 +00:00
|
|
|
"""Implementation of Basic Math in Python."""
|
2018-10-19 12:48:28 +00:00
|
|
|
import math
|
|
|
|
|
2019-07-02 04:05:43 +00:00
|
|
|
|
2019-10-29 20:54:31 +00:00
|
|
|
def prime_factors(n: int) -> list:
|
|
|
|
"""Find Prime Factors.
|
|
|
|
>>> prime_factors(100)
|
|
|
|
[2, 2, 5, 5]
|
2021-06-16 06:34:32 +00:00
|
|
|
>>> prime_factors(0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Only positive integers have prime factors
|
|
|
|
>>> prime_factors(-10)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Only positive integers have prime factors
|
2019-10-29 20:54:31 +00:00
|
|
|
"""
|
2021-06-16 06:34:32 +00:00
|
|
|
if n <= 0:
|
|
|
|
raise ValueError("Only positive integers have prime factors")
|
2018-10-19 12:48:28 +00:00
|
|
|
pf = []
|
|
|
|
while n % 2 == 0:
|
|
|
|
pf.append(2)
|
|
|
|
n = int(n / 2)
|
2019-07-02 04:05:43 +00:00
|
|
|
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
2018-10-19 12:48:28 +00:00
|
|
|
while n % i == 0:
|
|
|
|
pf.append(i)
|
|
|
|
n = int(n / i)
|
|
|
|
if n > 2:
|
|
|
|
pf.append(n)
|
|
|
|
return pf
|
|
|
|
|
2019-07-02 04:05:43 +00:00
|
|
|
|
2019-10-29 20:54:31 +00:00
|
|
|
def number_of_divisors(n: int) -> int:
|
|
|
|
"""Calculate Number of Divisors of an Integer.
|
|
|
|
>>> number_of_divisors(100)
|
|
|
|
9
|
2021-06-16 06:34:32 +00:00
|
|
|
>>> number_of_divisors(0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Only positive numbers are accepted
|
|
|
|
>>> number_of_divisors(-10)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Only positive numbers are accepted
|
2019-10-29 20:54:31 +00:00
|
|
|
"""
|
2021-06-16 06:34:32 +00:00
|
|
|
if n <= 0:
|
|
|
|
raise ValueError("Only positive numbers are accepted")
|
2018-10-19 12:48:28 +00:00
|
|
|
div = 1
|
|
|
|
temp = 1
|
|
|
|
while n % 2 == 0:
|
|
|
|
temp += 1
|
|
|
|
n = int(n / 2)
|
2019-10-29 20:54:31 +00:00
|
|
|
div *= temp
|
2019-07-02 04:05:43 +00:00
|
|
|
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
2018-10-19 12:48:28 +00:00
|
|
|
temp = 1
|
|
|
|
while n % i == 0:
|
|
|
|
temp += 1
|
|
|
|
n = int(n / i)
|
2019-10-29 20:54:31 +00:00
|
|
|
div *= temp
|
2018-10-19 12:48:28 +00:00
|
|
|
return div
|
|
|
|
|
2019-07-02 04:05:43 +00:00
|
|
|
|
2019-10-29 20:54:31 +00:00
|
|
|
def sum_of_divisors(n: int) -> int:
|
|
|
|
"""Calculate Sum of Divisors.
|
|
|
|
>>> sum_of_divisors(100)
|
|
|
|
217
|
2021-06-16 06:34:32 +00:00
|
|
|
>>> sum_of_divisors(0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Only positive numbers are accepted
|
|
|
|
>>> sum_of_divisors(-10)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Only positive numbers are accepted
|
2019-10-29 20:54:31 +00:00
|
|
|
"""
|
2021-06-16 06:34:32 +00:00
|
|
|
if n <= 0:
|
|
|
|
raise ValueError("Only positive numbers are accepted")
|
2018-10-19 12:48:28 +00:00
|
|
|
s = 1
|
|
|
|
temp = 1
|
|
|
|
while n % 2 == 0:
|
|
|
|
temp += 1
|
|
|
|
n = int(n / 2)
|
|
|
|
if temp > 1:
|
2019-10-05 05:14:13 +00:00
|
|
|
s *= (2 ** temp - 1) / (2 - 1)
|
2019-07-02 04:05:43 +00:00
|
|
|
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
2018-10-19 12:48:28 +00:00
|
|
|
temp = 1
|
|
|
|
while n % i == 0:
|
|
|
|
temp += 1
|
|
|
|
n = int(n / i)
|
|
|
|
if temp > 1:
|
2019-10-05 05:14:13 +00:00
|
|
|
s *= (i ** temp - 1) / (i - 1)
|
2019-10-29 20:54:31 +00:00
|
|
|
return int(s)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-02 04:05:43 +00:00
|
|
|
|
2019-10-29 20:54:31 +00:00
|
|
|
def euler_phi(n: int) -> int:
|
2020-03-04 12:40:28 +00:00
|
|
|
"""Calculate Euler's Phi Function.
|
2019-10-29 20:54:31 +00:00
|
|
|
>>> euler_phi(100)
|
|
|
|
40
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
s = n
|
2019-10-29 22:54:31 +00:00
|
|
|
for x in set(prime_factors(n)):
|
2019-07-02 04:05:43 +00:00
|
|
|
s *= (x - 1) / x
|
2019-10-29 20:54:31 +00:00
|
|
|
return int(s)
|
2019-07-02 04:05:43 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-29 20:54:31 +00:00
|
|
|
if __name__ == "__main__":
|
2021-06-16 06:34:32 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|