"""Implementation of Basic Math in Python."""import mathdef prime_factors(n: int) -> list:"""Find Prime Factors.>>> prime_factors(100)[2, 2, 5, 5]>>> 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"""if n <= 0:raise ValueError("Only positive integers have 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):while n % i == 0:pf.append(i)n = int(n / i)if n > 2:pf.append(n)return pfdef number_of_divisors(n: int) -> int:"""Calculate Number of Divisors of an Integer.>>> number_of_divisors(100)9>>> 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"""if n <= 0:raise ValueError("Only positive numbers are accepted")div = 1temp = 1while n % 2 == 0:temp += 1n = int(n / 2)div *= tempfor i in range(3, int(math.sqrt(n)) + 1, 2):temp = 1while n % i == 0:temp += 1n = int(n / i)div *= tempif n > 1:div *= 2return divdef sum_of_divisors(n: int) -> int:"""Calculate Sum of Divisors.>>> sum_of_divisors(100)217>>> 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"""if n <= 0:raise ValueError("Only positive numbers are accepted")s = 1temp = 1while n % 2 == 0:temp += 1n = int(n / 2)if temp > 1:s *= (2**temp - 1) / (2 - 1)for i in range(3, int(math.sqrt(n)) + 1, 2):temp = 1while n % i == 0:temp += 1n = 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>>> euler_phi(0)Traceback (most recent call last):...ValueError: Only positive numbers are accepted>>> euler_phi(-10)Traceback (most recent call last):...ValueError: Only positive numbers are accepted"""if n <= 0:raise ValueError("Only positive numbers are accepted")s = nfor x in set(prime_factors(n)):s *= (x - 1) / xreturn int(s)if __name__ == "__main__":import doctestdoctest.testmod()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型