mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
7f04e5cd34
* spelling corrections * review * improved documentation, removed redundant variables, added testing * added type hint * camel case to snake case * spelling fix * review * python --> Python # it is a brand name, not a snake * explicit cast to int * spaces in int list * "!= None" to "is not None" * Update comb_sort.py * various spelling corrections in documentation & several variables naming conventions fix * + char in file name * import dependency - bug fix Co-authored-by: John Law <johnlaw.po@gmail.com>
81 lines
1.7 KiB
Python
81 lines
1.7 KiB
Python
"""Implementation of Basic Math in Python."""
|
|
import math
|
|
|
|
|
|
def prime_factors(n: int) -> list:
|
|
"""Find Prime Factors.
|
|
>>> prime_factors(100)
|
|
[2, 2, 5, 5]
|
|
"""
|
|
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: int) -> int:
|
|
"""Calculate Number of Divisors of an Integer.
|
|
>>> number_of_divisors(100)
|
|
9
|
|
"""
|
|
div = 1
|
|
temp = 1
|
|
while n % 2 == 0:
|
|
temp += 1
|
|
n = int(n / 2)
|
|
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 *= temp
|
|
return div
|
|
|
|
|
|
def sum_of_divisors(n: int) -> int:
|
|
"""Calculate Sum of Divisors.
|
|
>>> sum_of_divisors(100)
|
|
217
|
|
"""
|
|
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 int(s)
|
|
|
|
|
|
def euler_phi(n: int) -> int:
|
|
"""Calculate Euler's Phi Function.
|
|
>>> euler_phi(100)
|
|
40
|
|
"""
|
|
s = n
|
|
for x in set(prime_factors(n)):
|
|
s *= (x - 1) / x
|
|
return int(s)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(prime_factors(100))
|
|
print(number_of_divisors(100))
|
|
print(sum_of_divisors(100))
|
|
print(euler_phi(100))
|