mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-20 05:29:48 +00:00
Update basic_maths.py (#1517)
* Update basic_maths.py Add Doctests. * Update basic_maths.py * Add a space to fix the doctest
This commit is contained in:
parent
4880e5479a
commit
f8e97aa597
@ -2,55 +2,56 @@
|
|||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
def prime_factors(n):
|
def prime_factors(n: int) -> list:
|
||||||
"""Find Prime Factors."""
|
"""Find Prime Factors.
|
||||||
|
>>> prime_factors(100)
|
||||||
|
[2, 2, 5, 5]
|
||||||
|
"""
|
||||||
pf = []
|
pf = []
|
||||||
while n % 2 == 0:
|
while n % 2 == 0:
|
||||||
pf.append(2)
|
pf.append(2)
|
||||||
n = int(n / 2)
|
n = int(n / 2)
|
||||||
|
|
||||||
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
||||||
while n % i == 0:
|
while n % i == 0:
|
||||||
pf.append(i)
|
pf.append(i)
|
||||||
n = int(n / i)
|
n = int(n / i)
|
||||||
|
|
||||||
if n > 2:
|
if n > 2:
|
||||||
pf.append(n)
|
pf.append(n)
|
||||||
|
|
||||||
return pf
|
return pf
|
||||||
|
|
||||||
|
|
||||||
def number_of_divisors(n):
|
def number_of_divisors(n: int) -> int:
|
||||||
"""Calculate Number of Divisors of an Integer."""
|
"""Calculate Number of Divisors of an Integer.
|
||||||
|
>>> number_of_divisors(100)
|
||||||
|
9
|
||||||
|
"""
|
||||||
div = 1
|
div = 1
|
||||||
|
|
||||||
temp = 1
|
temp = 1
|
||||||
while n % 2 == 0:
|
while n % 2 == 0:
|
||||||
temp += 1
|
temp += 1
|
||||||
n = int(n / 2)
|
n = int(n / 2)
|
||||||
div = div * (temp)
|
div *= temp
|
||||||
|
|
||||||
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
||||||
temp = 1
|
temp = 1
|
||||||
while n % i == 0:
|
while n % i == 0:
|
||||||
temp += 1
|
temp += 1
|
||||||
n = int(n / i)
|
n = int(n / i)
|
||||||
div = div * (temp)
|
div *= temp
|
||||||
|
|
||||||
return div
|
return div
|
||||||
|
|
||||||
|
|
||||||
def sum_of_divisors(n):
|
def sum_of_divisors(n: int) -> int:
|
||||||
"""Calculate Sum of Divisors."""
|
"""Calculate Sum of Divisors.
|
||||||
|
>>> sum_of_divisors(100)
|
||||||
|
217
|
||||||
|
"""
|
||||||
s = 1
|
s = 1
|
||||||
|
|
||||||
temp = 1
|
temp = 1
|
||||||
while n % 2 == 0:
|
while n % 2 == 0:
|
||||||
temp += 1
|
temp += 1
|
||||||
n = int(n / 2)
|
n = int(n / 2)
|
||||||
if temp > 1:
|
if temp > 1:
|
||||||
s *= (2 ** temp - 1) / (2 - 1)
|
s *= (2 ** temp - 1) / (2 - 1)
|
||||||
|
|
||||||
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
||||||
temp = 1
|
temp = 1
|
||||||
while n % i == 0:
|
while n % i == 0:
|
||||||
@ -58,27 +59,24 @@ def sum_of_divisors(n):
|
|||||||
n = int(n / i)
|
n = int(n / i)
|
||||||
if temp > 1:
|
if temp > 1:
|
||||||
s *= (i ** temp - 1) / (i - 1)
|
s *= (i ** temp - 1) / (i - 1)
|
||||||
|
return int(s)
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
def euler_phi(n):
|
def euler_phi(n: int) -> int:
|
||||||
"""Calculte Euler's Phi Function."""
|
"""Calculte Euler's Phi Function.
|
||||||
|
>>> euler_phi(100)
|
||||||
|
40
|
||||||
|
"""
|
||||||
l = prime_factors(n)
|
l = prime_factors(n)
|
||||||
l = set(l)
|
l = set(l)
|
||||||
s = n
|
s = n
|
||||||
for x in l:
|
for x in l:
|
||||||
s *= (x - 1) / x
|
s *= (x - 1) / x
|
||||||
return s
|
return int(s)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
if __name__ == "__main__":
|
||||||
"""Print the Results of Basic Math Operations."""
|
|
||||||
print(prime_factors(100))
|
print(prime_factors(100))
|
||||||
print(number_of_divisors(100))
|
print(number_of_divisors(100))
|
||||||
print(sum_of_divisors(100))
|
print(sum_of_divisors(100))
|
||||||
print(euler_phi(100))
|
print(euler_phi(100))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user