mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
Improve Formatting and Code Quality (#934)
* Improved Formatting of basic_maths.py - Added docstrings. - Improved whitespace formatting. - Renamed functions to match snake_case. * Improved Formatting of factorial_python.py - Added docstrings. - Improved whitespace formatting. - Renamed constants to match UPPER_CASE. * Improved Formatting of factorial_recursive.py - Improved whitespace formatting to meet PyLint standards. * Improved Code to Conform to PyLint - Renamed `max` to `max_num` to avoid redefining built-in 'max' [pylint] - Removed unnecessary parens after 'while' keyword [pylint] * Improved Formatting of factorial_recursive.py - Added docstrings. - Improved whitespace formatting.
This commit is contained in:

committed by
Anup Kumar Panwar

parent
bd4017928e
commit
a2236cfb97
@ -1,74 +1,84 @@
|
||||
"""Implementation of Basic Math in Python."""
|
||||
import math
|
||||
|
||||
def primeFactors(n):
|
||||
|
||||
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):
|
||||
|
||||
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 numberOfDivisors(n):
|
||||
|
||||
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):
|
||||
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 sumOfDivisors(n):
|
||||
|
||||
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):
|
||||
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 eulerPhi(n):
|
||||
l = primeFactors(n)
|
||||
|
||||
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
|
||||
s *= (x - 1) / x
|
||||
return s
|
||||
|
||||
|
||||
def main():
|
||||
print(primeFactors(100))
|
||||
print(numberOfDivisors(100))
|
||||
print(sumOfDivisors(100))
|
||||
print(eulerPhi(100))
|
||||
|
||||
"""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()
|
||||
|
||||
|
Reference in New Issue
Block a user