Compare commits

..

9 Commits

Author SHA1 Message Date
Sourabh kumar verma
896f6bebb0
Update prime_factorization_fast.py 2023-08-22 00:13:19 +05:30
Sourabh kumar verma
b64348256f
Update prime_factorization_fast.py 2023-08-22 00:11:53 +05:30
pre-commit-ci[bot]
332f34cba3 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-08-21 18:28:40 +00:00
Sourabh kumar verma
cfb91ebdef
Update prime_factors.py 2023-08-21 23:57:04 +05:30
Sourabh kumar verma
7f4e3e6967
Update prime_factorization_fast.py 2023-08-21 23:56:47 +05:30
pre-commit-ci[bot]
d81f0a3cc8 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-08-21 18:04:35 +00:00
Sourabh kumar verma
e36b7b7640
Update prime_factorization_fast.py 2023-08-21 23:34:15 +05:30
Sourabh kumar verma
9cc02c8ba7
Update prime_factors.py 2023-08-21 23:34:01 +05:30
Sourabh kumar verma
ed509591e4
Update prime_factors.py 2023-08-21 23:28:03 +05:30
2 changed files with 55 additions and 44 deletions

View File

@ -1,23 +1,11 @@
from __future__ import annotations
from math import ceil, sqrt
import time
from typing import Any
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]:
def prime_factors(num: int) -> list[int]:
"""
Returns prime factors of a given number as a list.
Returns prime factors of n as a list.
>>> prime_factors(0)
@ -45,41 +33,36 @@ def prime_factors(n: int) -> list[int]:
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:
while i * i <= num:
if num % i:
i += 1
else:
n //= i
num //= i
factors.append(i)
if n > 1:
factors.append(n)
if num > 1:
factors.append(num)
return factors
@timer
def primeproduct(num: int) -> list[int]:
"""
Returns prime factors of a positive integer num as a list.
2 is only even prime so special while loop to handle it.
Skipping other even numbers.
Taking root n approach for getting prime number.
>>> primeproduct(868)
[2, 2, 7, 31]
>>> primeproduct(9039423423423743)
[2, 2, 7, 31, 719, 12572216166097]
>>> primeproduct(0.02)
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: '0.02'
[7, 719, 1796030880871]
>>> primeproduct(435345234543252)
[2, 2, 3, 3, 3, 11, 3119, 5171, 22721]
>>> primeproduct(-2342)
[]
"""
if not isinstance(num, int):
raise TypeError("Input must be an integer")
"""
if num <= 1:
return []
@ -87,6 +70,7 @@ def primeproduct(num: int) -> list[int]:
while num > 1:
if len(prime_factors) >= 1 and prime_factors[-1] % num == 0:
prime_factors.append(prime_factors[-1])
else:
sq = ceil(sqrt(num))
flag = 0
@ -98,20 +82,24 @@ def primeproduct(num: int) -> list[int]:
prime_factors.append(i)
flag = 1
break
else:
while num % 2 == 0:
num = num // 2
prime_factors.append(2)
for i in range(3, sq + 1, 2):
if num % i == 0:
num = num // i
prime_factors.append(i)
flag = 1
break
if not flag and num > 1:
prime_factors.append(num)
num = 1
break
return prime_factors
@ -119,3 +107,6 @@ if __name__ == "__main__":
n = int(input("enter number: ").strip())
primeproduct(n)
prime_factors(n)
import doctest
doctest.testmod()

View File

@ -1,13 +1,28 @@
from __future__ import annotations
from math import ceil, sqrt
import time
from typing import Any
def timer(func):
def wrapper(*args, **kwargs):
def timer(func: callable) -> callable:
"""
Decorator that measures the execution time of a function.
:param func: The function to be timed.
:return: The wrapped function.
"""
def wrapper(*args: Any, **kwargs: Any) -> Any:
"""
Wraps the given function and measures its execution time.
:param args: Positional arguments for the function.
:param kwargs: Keyword arguments for the function.
:return: The result of the wrapped function.
"""
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
@ -16,8 +31,9 @@ def timer(func):
@timer
def prime_factors(n: int) -> list[int]:
def prime_factors(number: int) -> list[int]:
"""
Returns prime factors of a given number as a list.
Returns prime factors of n as a list.
>>> prime_factors(0)
@ -47,20 +63,22 @@ def prime_factors(n: int) -> list[int]:
"""
i = 2
factors = []
while i * i <= n:
if n % i:
while i * i <= number:
if number % i:
i += 1
else:
n //= i
number //= i
factors.append(i)
if n > 1:
factors.append(n)
if number > 1:
factors.append(number)
return factors
@timer
def primeproduct(num: int) -> list[int]:
"""
Returns prime factors of a positive integer num as a list.
>>> primeproduct(868)
[2, 2, 7, 31]
>>> primeproduct(9039423423423743)
@ -70,8 +88,8 @@ def primeproduct(num: int) -> list[int]:
ValueError: invalid literal for int() with base 10: '0.02'
>>> primeproduct(-2342)
[]
"""
"""
if num <= 1:
return []
@ -118,4 +136,6 @@ if __name__ == "__main__":
prime_factors(n)
import doctest
doctest.NORMALIZE_WHITESPACE
doctest.testmod()