mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
8bd2067393
commit
bb34d5d215
@ -1,20 +1,21 @@
|
|||||||
from math import ceil , sqrt
|
from math import ceil, sqrt
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
def primeproduct(n : int, x : list = []):
|
|
||||||
'''
|
def primeproduct(n: int, x: list = []):
|
||||||
|
"""
|
||||||
>>> primeproduct(868)
|
>>> primeproduct(868)
|
||||||
[2, 2, 7, 31]
|
[2, 2, 7, 31]
|
||||||
>>> primeproduct(9039423423423743)
|
>>> primeproduct(9039423423423743)
|
||||||
[2, 2, 7, 31, 719, 12572216166097]
|
[2, 2, 7, 31, 719, 12572216166097]
|
||||||
>>> primeproduct(0.02)
|
>>> primeproduct(0.02)
|
||||||
[]
|
[]
|
||||||
'''
|
"""
|
||||||
if n < 1:
|
if n < 1:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
if n > 1:
|
if n > 1:
|
||||||
if len(x)>=1 and x[-1] % n == 0: # check in already factorised
|
if len(x) >= 1 and x[-1] % n == 0: # check in already factorised
|
||||||
x.append(x[-1])
|
x.append(x[-1])
|
||||||
n = n // x[-1]
|
n = n // x[-1]
|
||||||
|
|
||||||
@ -23,22 +24,21 @@ def primeproduct(n : int, x : list = []):
|
|||||||
flag = 0
|
flag = 0
|
||||||
|
|
||||||
if x != []:
|
if x != []:
|
||||||
for i in range(x[-1] , sq+1, 2):
|
for i in range(x[-1], sq + 1, 2):
|
||||||
if n % i == 0:
|
if n % i == 0:
|
||||||
n = n // i
|
n = n // i
|
||||||
x.append(i)
|
x.append(i)
|
||||||
flag = 1
|
flag = 1
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Handle factor 2 separately
|
# Handle factor 2 separately
|
||||||
while n % 2 ==0: # only 2 is even prime
|
while n % 2 == 0: # only 2 is even prime
|
||||||
n = n // 2
|
n = n // 2
|
||||||
x.append(2)
|
x.append(2)
|
||||||
|
|
||||||
# Start loop from 3 and increment by 2
|
# Start loop from 3 and increment by 2
|
||||||
for i in range(3 , sq+1, 2): # skip even numbers
|
for i in range(3, sq + 1, 2): # skip even numbers
|
||||||
if n % i == 0:
|
if n % i == 0:
|
||||||
n = n // i
|
n = n // i
|
||||||
x.append(i)
|
x.append(i)
|
||||||
@ -49,10 +49,11 @@ def primeproduct(n : int, x : list = []):
|
|||||||
x.append(n)
|
x.append(n)
|
||||||
n = 1
|
n = 1
|
||||||
|
|
||||||
return primeproduct(n,x)
|
return primeproduct(n, x)
|
||||||
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
# faster than https://github.com/sourabhkv/Python/blob/master/maths/prime_factors.py approx 2x
|
# faster than https://github.com/sourabhkv/Python/blob/master/maths/prime_factors.py approx 2x
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
Loading…
x
Reference in New Issue
Block a user