im trying to get the factor prime number(s) for given number if its different than the primes that i have in a list.
The number have to:
dividable with any of the primes in the list
should not have any other factor as primes other than in the list
example:
my_primes = [2, 3, 5]
n = 14 should return [7]
14 has factors [1, 2, 7, 14], can be divided by prime 2 (which is in list and prime) but also have 7 as factor prime and it is not in my list so i want to return that. If number has even 1 prime like this its enough for me so i dont need to check till i find other primes.
The code i have come up with so far:
from rTime import timer
@timer
def solve(n):
def is_prime(n):
i = 2
while i**2 <= n:
if n % i == 0:
return False
i += 1
return True
primes = [2, 3, 5]
listo = []
if n % 2 != 0 and n % 3 != 0 and n % 5 != 0:
return False
for i in range(2, n):
if n % i == 0 and is_prime(i) and i not in primes:
listo.append(i)
if listo:
break
return listo
result = solve(1926576016)
print(result)
so my problem is it takes 22.36 seconds to check if 1926576016 has any primes like that at the moment. Is there anything i can improve in my code to make it faster or whole different approach needed to do this task.
Should note that i have been learning python since 4-5 months so i might not be aware of any build-in tools that makes this faster and its my first question here sorry if i did something wrong !
(@timer is a function that i have write to check how long it takes to run a function)