mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Compare commits
9 Commits
0f193400e1
...
896f6bebb0
Author | SHA1 | Date | |
---|---|---|---|
|
896f6bebb0 | ||
|
b64348256f | ||
|
332f34cba3 | ||
|
cfb91ebdef | ||
|
7f4e3e6967 | ||
|
d81f0a3cc8 | ||
|
e36b7b7640 | ||
|
9cc02c8ba7 | ||
|
ed509591e4 |
@ -1,23 +1,11 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from math import ceil, sqrt
|
from math import ceil, sqrt
|
||||||
import time
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
def timer(func):
|
def prime_factors(num: int) -> list[int]:
|
||||||
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 a given number as a list.
|
||||||
Returns prime factors of n as a list.
|
Returns prime factors of n as a list.
|
||||||
|
|
||||||
>>> prime_factors(0)
|
>>> prime_factors(0)
|
||||||
@ -45,41 +33,36 @@ def prime_factors(n: int) -> list[int]:
|
|||||||
TypeError: '<=' not supported between instances of 'int' and 'list'
|
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
|
i = 2
|
||||||
factors = []
|
factors = []
|
||||||
while i * i <= n:
|
while i * i <= num:
|
||||||
if n % i:
|
if num % i:
|
||||||
i += 1
|
i += 1
|
||||||
else:
|
else:
|
||||||
n //= i
|
num //= i
|
||||||
factors.append(i)
|
factors.append(i)
|
||||||
if n > 1:
|
if num > 1:
|
||||||
factors.append(n)
|
factors.append(num)
|
||||||
return factors
|
return factors
|
||||||
|
|
||||||
|
|
||||||
@timer
|
|
||||||
def primeproduct(num: int) -> list[int]:
|
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)
|
>>> primeproduct(868)
|
||||||
[2, 2, 7, 31]
|
[2, 2, 7, 31]
|
||||||
>>> primeproduct(9039423423423743)
|
>>> primeproduct(9039423423423743)
|
||||||
[2, 2, 7, 31, 719, 12572216166097]
|
[7, 719, 1796030880871]
|
||||||
>>> primeproduct(0.02)
|
>>> primeproduct(435345234543252)
|
||||||
Traceback (most recent call last):
|
[2, 2, 3, 3, 3, 11, 3119, 5171, 22721]
|
||||||
ValueError: invalid literal for int() with base 10: '0.02'
|
|
||||||
>>> primeproduct(-2342)
|
>>> primeproduct(-2342)
|
||||||
[]
|
[]
|
||||||
"""
|
|
||||||
if not isinstance(num, int):
|
|
||||||
raise TypeError("Input must be an integer")
|
|
||||||
|
|
||||||
|
"""
|
||||||
if num <= 1:
|
if num <= 1:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@ -87,6 +70,7 @@ 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
|
||||||
@ -98,20 +82,24 @@ 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
|
||||||
|
|
||||||
|
|
||||||
@ -119,3 +107,6 @@ if __name__ == "__main__":
|
|||||||
n = int(input("enter number: ").strip())
|
n = int(input("enter number: ").strip())
|
||||||
primeproduct(n)
|
primeproduct(n)
|
||||||
prime_factors(n)
|
prime_factors(n)
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
@ -1,13 +1,28 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from math import ceil, sqrt
|
from math import ceil, sqrt
|
||||||
import time
|
import time
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
def timer(func):
|
def timer(func: callable) -> callable:
|
||||||
def wrapper(*args, **kwargs):
|
"""
|
||||||
|
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()
|
start = time.time()
|
||||||
result = func(*args, **kwargs)
|
result = func(*args, **kwargs)
|
||||||
print(result)
|
|
||||||
end = time.time()
|
end = time.time()
|
||||||
print(f"{func.__name__} took {end - start:.6f} seconds to execute.")
|
print(f"{func.__name__} took {end - start:.6f} seconds to execute.")
|
||||||
return result
|
return result
|
||||||
@ -16,8 +31,9 @@ def timer(func):
|
|||||||
|
|
||||||
|
|
||||||
@timer
|
@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.
|
Returns prime factors of n as a list.
|
||||||
|
|
||||||
>>> prime_factors(0)
|
>>> prime_factors(0)
|
||||||
@ -47,20 +63,22 @@ def prime_factors(n: int) -> list[int]:
|
|||||||
"""
|
"""
|
||||||
i = 2
|
i = 2
|
||||||
factors = []
|
factors = []
|
||||||
while i * i <= n:
|
while i * i <= number:
|
||||||
if n % i:
|
if number % i:
|
||||||
i += 1
|
i += 1
|
||||||
else:
|
else:
|
||||||
n //= i
|
number //= i
|
||||||
factors.append(i)
|
factors.append(i)
|
||||||
if n > 1:
|
if number > 1:
|
||||||
factors.append(n)
|
factors.append(number)
|
||||||
return factors
|
return factors
|
||||||
|
|
||||||
|
|
||||||
@timer
|
@timer
|
||||||
def primeproduct(num: int) -> list[int]:
|
def primeproduct(num: int) -> list[int]:
|
||||||
"""
|
"""
|
||||||
|
Returns prime factors of a positive integer num as a list.
|
||||||
|
|
||||||
>>> primeproduct(868)
|
>>> primeproduct(868)
|
||||||
[2, 2, 7, 31]
|
[2, 2, 7, 31]
|
||||||
>>> primeproduct(9039423423423743)
|
>>> primeproduct(9039423423423743)
|
||||||
@ -70,8 +88,8 @@ def primeproduct(num: int) -> list[int]:
|
|||||||
ValueError: invalid literal for int() with base 10: '0.02'
|
ValueError: invalid literal for int() with base 10: '0.02'
|
||||||
>>> primeproduct(-2342)
|
>>> primeproduct(-2342)
|
||||||
[]
|
[]
|
||||||
"""
|
|
||||||
|
|
||||||
|
"""
|
||||||
if num <= 1:
|
if num <= 1:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@ -118,4 +136,6 @@ if __name__ == "__main__":
|
|||||||
prime_factors(n)
|
prime_factors(n)
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
|
doctest.NORMALIZE_WHITESPACE
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user