Update prime_factorization_fast.py

This commit is contained in:
Sourabh kumar verma 2023-08-06 23:15:19 +05:30 committed by GitHub
parent a94659c1b0
commit c18681669a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,66 @@
from __future__ import annotations from __future__ import annotations
from math import ceil, sqrt from math import ceil, sqrt
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print(result)
end = time.time()
print(f"{func.__name__} took {end - start:.6f} seconds to execute.")
return result
return wrapper
@timer
def prime_factors(n: int) -> list[int]:
"""
Returns prime factors of n as a list.
>>> prime_factors(0)
[]
>>> prime_factors(100)
[2, 2, 5, 5]
>>> prime_factors(2560)
[2, 2, 2, 2, 2, 2, 2, 2, 2, 5]
>>> prime_factors(10**-2)
[]
>>> prime_factors(0.02)
[]
>>> x = prime_factors(10**241) # doctest: +NORMALIZE_WHITESPACE
>>> x == [2]*241 + [5]*241
True
>>> prime_factors(10**-354)
[]
>>> prime_factors('hello')
Traceback (most recent call last):
...
TypeError: '<=' not supported between instances of 'int' and 'str'
>>> prime_factors([1,2,'hello'])
Traceback (most recent call last):
...
TypeError: '<=' not supported between instances of 'int' and 'list'
"""
if not isinstance(n, int):
raise TypeError("Input must be an integer")
if n <= 0:
return []
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors
@timer
def primeproduct(num: int) -> list[int]: def primeproduct(num: int) -> list[int]:
""" """
>>> primeproduct(868) >>> primeproduct(868)
@ -14,6 +73,8 @@ def primeproduct(num: int) -> list[int]:
>>> primeproduct(-2342) >>> primeproduct(-2342)
[] []
""" """
if not isinstance(num, int):
raise TypeError("Input must be an integer")
if num <= 1: if num <= 1:
return [] return []
@ -22,7 +83,6 @@ def primeproduct(num: int) -> list[int]:
while num > 1: while num > 1:
if len(prime_factors) >= 1 and prime_factors[-1] % num == 0: if len(prime_factors) >= 1 and prime_factors[-1] % num == 0:
prime_factors.append(prime_factors[-1]) prime_factors.append(prime_factors[-1])
else: else:
sq = ceil(sqrt(num)) sq = ceil(sqrt(num))
flag = 0 flag = 0
@ -34,28 +94,23 @@ def primeproduct(num: int) -> list[int]:
prime_factors.append(i) prime_factors.append(i)
flag = 1 flag = 1
break break
else: else:
while num % 2 == 0: while num % 2 == 0:
num = num // 2 num = num // 2
prime_factors.append(2) prime_factors.append(2)
for i in range(3, sq + 1, 2): for i in range(3, sq + 1, 2):
if num % i == 0: if num % i == 0:
num = num // i num = num // i
prime_factors.append(i) prime_factors.append(i)
flag = 1 flag = 1
break break
if not flag and num > 1: if not flag and num > 1:
prime_factors.append(num) prime_factors.append(num)
num = 1 num = 1
break break
return prime_factors return prime_factors
if __name__ == "__main__": if __name__ == "__main__":
import doctest n = int(input('enter number: ').strip())
primeproduct(n)
doctest.testmod() prime_factors(n)