Looking to bring runtime under 20 seconds - python

The following program, balanced_centrifuge(), takes positive integers (n, k), where k <= n, and determines whether k and n-k can be expressed as a sum of the prime factors of n (note, repetition of factors is allowed). I've been able to walk through the logic of this code and it makes sense, but the runtime is over 20 seconds which is too slow for my tester. It works for all the suggested test cases however, (6,3), (7,0), (15,8), (222, 107), and (1234, 43). Where is the slowdown occurring?
import itertools
def balanced_centrifuge(n,k):
if n == 1:
return False
if k == 0 or n - k == 0:
return True
primes = get_primes(n)
final_answer = is_sum(primes, k) and is_sum(primes,n-k)
return final_answer
def get_primes(n):
i = 2
factors = []
while i * i <= n:
if n % i != 0:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors
def is_sum(primes, goal):
if primes == []:
return False
for i in range(goal//primes[0]+1, 0, -1):
ans = itertools.combinations_with_replacement(primes, i)
for comb in ans:
#print(comb)
# single tuple with i elements to try
if sum(comb) == goal:
return True
return False

Related

Improving list comprehension so that it runs faster

I'm creating a Goldbach conjecture which outputs the two prime numbers with the highest product. As it stands once the input is a large number, it takes a ridiculously long time to solve.
def prime_pair_solution(n):
def isprime(n):
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
if not n & 1:
return False
k= int(n**0.5)+1
for x in range(3, k, 2):
if n % x == 0:
return False
return True
count=(int(n/2)+(int(n/4)))
y=[(p2,p1)for p1 in range (2,count) for p2 in range(p1,count) if isprime(p1) and isprime(p2) and (p1+p2)==n]
return y[-1]

Count prime results of polynomial

Can someone help me and tell me why this doesn't work? The goal is to count the number of prime numbers that are produced by a given polynomial for inputs n in a specified range [a,b]:
def count_primes(poly, a, b):
primes = 0
if b >= a:
for n in range(a, b):
result = poly(n)
if result > 1:
for i in range(2, result):
if (result % i) == 0:
break
else:
primes += 1
else:
break
return primes
def poly(n):
return n**2 + n + 41
print(count_primes(poly, 0, 39))
The result should return 40 in this case.
[2] Problem Solution
Step-1. Take in the number to be checked and store it in a variable.
Step-2. Initialize the count variable to 0.
Step-3. Let the for loop range from 2 to half of the number (excluding 1 and the number itself).
Step-4. Then find the number of divisors using the if statement and increment the count variable each time.
Step-5. If the number of divisors is lesser than or equal to 0, the number is prime.
Step-6. Print the final result.
Step-7. Exit.
The problem is that the else clause in the nested loop refers to the if, when it should be activated only if the loop finished without break. Just change the identitation to:
if result > 1:
for i in range(2, result):
if (result % i) == 0:
break
else:
primes += 1
This is the wrong way to count primes:
if result > 1:
for i in range(2, result):
if (result % i) == 0:
break
else:
primes += 1
Should be:
if result > 1:
isPrime = True
for i in range(2, result):
if (result % i) == 0:
isPrime = False
break
if isPrime:
primes += 1
Also, it goes without saying. Easy optimizations for prime number detection. You only have to test for divisibility divisibility with 2 and all odd numbers between 3 and sqrt(result).
def count_primes(poly, a, b):
primes = 0
if b >= a:
for n in range(a, b+1):
result = poly(n)
if result > 1:
for i in range(2, result):
if (result % i) == 0:
break
else:
primes += 1
else:
break
return primes
def poly(n):
return n**2 + n + 41
print(count_primes(poly, 0, 39))
Problems:
you do primes += 1 too early. In your methods, you have to test until no possible division happens, then do the addition.
[a, b] The endpoints are both inclusive. Then you should use b+1 at your for n in range(a, b+1), which produces 40.

Check if a number can be divided into prime partitions

Can somebody solve this problem on Python ?
A positive integer m can be partitioned as primes if it can be written as p + q where p > 0, q > 0 and both p and q are prime numbers.
Write a Python function that takes an integer m as input and returns True if m can be partitioned as primes and False otherwise.
Tried this, but does not work for all testcases, for example it should return True for 3432, it returns False.
def partition(num):
primelist=[]
for i in range(2,num + 1):
for p in range(2,i):
if (i % p) == 0:
break
else:
primelist.append(i)
for x in primelist:
y= num-x
for z in range(2,y):
if y%z == 0:
return False
return True
The error lies in the second for loop. You are looping through possible primes x, and wish to then check that y = num - x is also prime.
The error in your logic is that in the second for loop, if the first element in the loop y = num - x is not prime, it will return False, without checking any of the other possible values.
You could correct this by moving the return False statement out one loop, but since you have already generated a list of primes less than num, primelist (and since y = num - x, (if prime y exists) it will be in this list), you can just check for membership of the list:
for x in primelist:
y= num-x
# Note: num = x + y, thus need only check y prime
if y in primelist:
return True
# If no such y is prime, not possible
else:
return False
Note: I would advise making the logic of your script more modular, separating out the prime list generator into its own function:
def partition(num):
"""
Return True if there exist primes x,y such that num = x + y.
Else return False.
"""
primelist = primes(num)
for x in primelist:
y= num-x
# Note: num = x + y, thus need only check y prime
if y in primelist:
return True
# If no such y is prime, not possible
else:
return False
def primes(num):
"""Return list of all primes less than num."""
primelist=[]
for i in range(2,num + 1):
for p in range(2,i):
if (i % p) == 0:
break
else:
primelist.append(i)
return primelist
final solution i got:
def primepartition(m):
primelist=[]
if m<0:
return False
else:
for i in range(2,m + 1):
for p in range(2,i):
if (i % p) == 0:
break
else:
primelist.append(i)
for x in primelist:
y= m-x
if y in primelist:
return True
return False
The given below code can hopefully give you the correct output.
def factors(n):
factorslist = []
for i in range(1, n+1, 1):
if n % i == 0:
factorslist.append(i)
return(factorslist)
def prime(n):
if factors(n) == [1, n] and n > 1:
return(True)
def primelist(n):
primenolist = []
for i in range(1, n+1, 1):
if prime(i) == True:
primenolist.append(i)
return(primenolist)
def primepartition(m):
if m > 0:
primenolist = primelist(m)
checklist = []
for p in primenolist:
q = m - p
if q in primenolist and p > 0 and q > 0:
checklist.append((p,q))
if len(checklist) > 0:
return(True)
else:
return(False)
else:
return(False)
Another approach,
Initially, we store all prime elements upto m and check for pair of primes whose sum equal to m
def primepartition(a):
l=[2]#since 'a' should be >=2 for below loops, we took here 2(1st prime).
for i in range(2,a):
flag=0
for j in range(2,i):
if i%j==0:
flag=0
break
else:
flag=1
if flag==1:
l.append(i)
for i in l:
for j in l:
if i+j==a:
return True
return False
n=int(input("Enter any number: "))
list=[]
for num in range(0,n + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
list.append(num)
if (n<= 1):
print("False")
#print("It is not positive ")
else:
for i in list:
y = num -i
if (y in list):
print("True")
#print(y,"+",i,"=",n)
#print(i,"+",y,"=",n)
#print("The number can be expressed as the sum of two prime numbers.")
break
else:
print("False")
#print("The number can not be expressed as the sum of two prime numbers.")
Slight Variation of your code:
def primepartition0(m):
primelist=[]
if m<0:
return False
else:
for i in range(2,m + 1):
for p in range(2,i):
if (i % p) == 0:
break
else:
primelist.append(i)
for x in primelist:
for y in primelist:
if x != y and x+y == m:
return True
return False
An alternate approach that attemps to reduce the amount of code necessary:
def primepartition(m):
if m > 3:
for number in range(m // 2, m - 1):
difference = m - number
for psuedoprime in range(2, int(number ** 0.5) + 1):
if number % psuedoprime == 0 or difference > psuedoprime and difference % psuedoprime == 0:
break
else: # no break
return number, difference # as good a non-False result as any other...
return False
def factors(n):
factlist = []
for i in range(1,n+1):
# Since factors of 2 cannot be primes, we ignore them.
if n%i==0 and i%2!=0:
factlist.append(i)
return factlist
def isprime(n):
return(factors(n)==[1,n])
def preimesupto(n):
primelist = []
if n>=2:
primelist.append(2)
for i in range(n):
if isprime(i):
primelist.append(i)
return primelist
def primepartition(n):
if n<0:
return False
primelist = preimesupto(n)
for i in primelist:
j = n-i
if j in primelist:
return True
else:
return False
If you're not required to produce the actual primes but only test if there exists a pair of primes p and q such that p+q == N, you could make this very simple based on the Goldbach conjecture. All even numbers can be expressed as the sum of two primes. So return True if the number is even and check if N-2 is prime for odd numbers (because 2 is the only even prime and that's the only prime that will produce another odd number when starting from an odd number). This will boil down to a single prime test of N-2 only for odd numbers.
def primePart(N):
return N%2==0 or all((N-2)%p for p in range(3,int(N**0.5)+1,2))
primePart(3432) # True
primePart(37+2) # True
primePart(13+41) # True
primePart(123) # False
If you want to actually find a pair of primes that add up to N, you can generate primes up to N and return the first prime >= N/2 where N - prime is one of the primes already found:
def findPQ(N):
if not primePart(N): return
if N%2: return 2,N-2
isPrime = [0]+[1]*N
for p in range(3,N,2):
if not isPrime[p]: continue
if 2*p>=N and isPrime[N-p]: return p,N-p
isPrime[p*p::p] = [0]*len(isPrime[p*p::p])
output:
findPQ(3432) # (1723, 1709)
findPQ(12345678) # (6172879, 6172799)
To go beyond 10^9 you will need a more memory efficient algorithm than the sieve of Eratosthenes that is just as fast. This can be achieved with a dictionary of multiples of primes to skip:
def findPQ(N):
if not primePart(N): return
if N%2: return 2,N-2
skip,primes = {},{2}
for p in range(3,N,2):
if p in skip:
prime = skip.pop(p)
mult = p + 2*prime
while mult in skip: mult += 2*prime
if mult <= N: skip[mult] = prime
else:
if 2*p>=N and N-p in primes: return p,N-p
if p*p<=N: skip[p*p]=p
if 2*p<=N: primes.add(p)
output (takes a while but doesn't bust memory space):
findPQ(1234567890) # (617283983, 617283907)
def checkprime(number):
fact=1
for r in range(2,number):
if number%r==0:
fact=fact+1
return(fact<2)
def primepartition(m):
for i in range(2,m):
flag=0
if checkprime(i) and checkprime(m-i)==True:
flag=1
break
return(flag==1)
def matched(s):
list_of_string=list(s)
for y in range(len(list_of_string)):
if list_of_string[y]=='(':
for z in range(y,len(list_of_string)):
if list_of_string[z]==')':
list_of_string[y]='#'
list_of_string[z]='#'
break
return('('not in list_of_string and ')'not in list_of_string)
def rotatelist(l,k):
if k>len(l):
k=int(k%len(l))
return(l[k:]+l[0:k])

Algorithm of finding numbers

Write a recursive algorithm which enumerates dominant primes. Your algorithm should print dominant primes as it finds them (rather than at the end).By default we limit the dominant primes we are looking for to a maximum value of 10^12, the expected run time should be around or less than a minute.
The following is my python code which doesn't work as expected:
import math
def test_prime(n):
k = 2
maxk = int(math.sqrt(n))
while k <= maxk:
if n % k == 0:
return False
if k == 2:
k += 1
else:
k += 2
return (n is not 1)
def dominant_prime_finder(maxprime=10**12,n=1):
l = 1 #length of the current number
while n // 10 > 0:
l += 1
n //= 10
if test_prime(n) == True:
is_dominant_prime = True
index_smaller = n
while l > 1 and index_smaller > 9:
index_smaller //= 10
if test_prime(index_smaller) == False:
is_dominant_prime = False
break
for i in range(1,10):
if test_prime(i*10**l + n) == True:
is_dominant_prime = False
break
if is_dominant_prime == True:
print(n)
while n <= maxprime:
dominant_prime_finder()
You can solve the problem without enumerating all the numbers under 10^12 which is inefficient by doing a recursion on the length of the number.
It works the following way:
The prime number of length 1 are: 2,3,5,7.
For all these numbers check the third condition, for any digit dn+1∈{1,…,9} , dn+1dn…d0 is not prime. For 2 it's okay. For 3 it fails (13 for instance). Store all the prime you find in a list L. Do this for all the prime of length 1.
In L you now have all the prime number of length 2 with a prime as first digit, thus you have all the candidates for dominant prime of length 2
Doing this recursively gets you all the dominant prime, in python:
def test_prime(n):
k = 2
maxk = int(math.sqrt(n))
while k <= maxk:
if n % k == 0:
return False
if k == 2:
k += 1
else:
k += 2
return (n is not 1)
def check_add_digit(number,length):
res = []
for i in range(1,10):
if test_prime( i*10**(length) + number ):
res.append(i*10**(length) + number)
return res
def one_step(list_prime,length):
## Under 10^12
if length > 12:
return None
for prime in list_prime:
res = check_add_digit(prime,length)
if len(res) == 0:
#We have a dominant prime stop
print(prime)
else:
#We do not have a dominant prime but a list of candidates for length +1
one_step(res,length+1)
one_step([2,3,5,7], length=1)
This works in under a minute on my machine.
Well, there are several issues with this code:
You modify the original n at the beginning (n //= 10). This basically causes n to always be one digit. Use another variable instead:
m = n
while m // 10 > 0:
l += 1
m //= 10
Your recursive call doesn't update n, so you enter an infinite loop:
while n <= maxprime:
dominant_prime_finder()
Replace with:
if n <= maxprime:
dominant_prime_finder(maxprime, n + 1)
Even after fixing these issues, you'll cause a stack overflow simply because the dominant prime numbers are very far apart (2, 5, 3733, 59399...). So instead of using a recursive approach, use, for example, a generator:
def dominant_prime_finder(n=1):
while True:
l = 1 #length of the current number
m = n
while m // 10 > 0:
l += 1
m //= 10
if test_prime(n):
is_dominant_prime = True
index_smaller = n
while l > 1 and index_smaller > 9:
index_smaller //= 10
if not test_prime(index_smaller):
is_dominant_prime = False
break
for i in range(1,10):
if test_prime(i*10**l + n):
is_dominant_prime = False
break
if is_dominant_prime:
yield n
n = n + 1
g = dominant_prime_finder()
for i in range(1, 10): # find first 10 dominant primes
print(g.next())
This problem is cool. Here's how we can elaborate the recurrence:
def dominant_prime_finder(maxprime=10**12):
def f(n):
if n > maxprime:
return
is_dominant = True
power = 10**(math.floor(math.log(n, 10)) + 1)
for d in xrange(1, 10):
candidate = d * power + n
if test_prime(candidate):
f(candidate)
is_dominant = False
if is_dominant:
print int(n)
for i in [2,3,5,7]:
f(i)

Finding the greatest prime divisor (the fastest program possible)

I was checking the problems on http://projecteuler.net/
The third problem is as follows:
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest
prime factor of the number 600851475143 ?
My solution code is below. But it is so slow that I think it will take weeks to complete.
How can I improve it? Or is Python itself too slow to solve this problem?
def IsPrime(num):
if num < 2:
return False
if num == 2:
return True
else:
for div in range(2,num):
if num % div == 0:
return False
return True
GetInput = int (input ("Enter the number: "))
PrimeDivisors = []
for i in range(GetInput, 1, -1):
print(i)
if GetInput % i == 0 and IsPrime(i) is True:
PrimeDivisors.append(i)
break
else:
continue
print(PrimeDivisors)
print("The greatest prime divisor is:", max(PrimeDivisors))
The problem with your solution is that you don't take your found prime factors into account, so you're needlessly checking factors after you've actually found the largest one. Here's my solution:
def largest_prime_factor(n):
largest = None
for i in range(2, n):
while n % i == 0:
largest = i
n //= i
if n == 1:
return largest
if n > 1:
return n
Project Euler problems are more about mathematics than programming, so if your solution is too slow, it's probably not your language that's at fault.
Note that my solution works quickly for this specific number by chance, so it's definitely not a general solution. Faster solutions are complicated and overkill in this specific case.
This might not be the fastest algorithm but it's quite efficient:
def prime(x):
if x in [0, 1]:
return False
for n in xrange(2, int(x ** 0.5 + 1)):
if x % n == 0:
return False
return True
def primes():
"""Prime Number Generator
Generator an infinite sequence of primes
http://stackoverflow.com/questions/567222/simple-prime-generator-in-python
"""
# Maps composites to primes witnessing their compositeness.
# This is memory efficient, as the sieve is not "run forward"
# indefinitely, but only as long as required by the current
# number being tested.
#
D = {}
# The running integer that's checked for primeness
q = 2
while True:
if q not in D:
# q is a new prime.
# Yield it and mark its first multiple that isn't
# already marked in previous iterations
#
yield q
D[q * q] = [q]
else:
# q is composite. D[q] is the list of primes that
# divide it. Since we've reached q, we no longer
# need it in the map, but we'll mark the next
# multiples of its witnesses to prepare for larger
# numbers
#
for p in D[q]:
D.setdefault(p + q, []).append(p)
del D[q]
q += 1
def primefactors(x):
if x in [0, 1]:
yield x
elif prime(x):
yield x
else:
for n in primes():
if x % n == 0:
yield n
break
for factor in primefactors(x // n):
yield factor
Usage:
>>> list(primefactors(100))
[2, 2, 5, 5]
My code which seems enough fast to me. Using collections.defaultdict() would make the code of primes() abit cleaner but I guess the code would loose some speed due to importing it.
def primes():
"""Prime number generator."""
n, skip = 2, {}
while True:
primes = skip.get(n)
if primes:
for p in primes:
skip.setdefault(n + p, set()).add(p)
del skip[n]
else:
yield n
skip[n * n] = {n}
n += 1
def un_factor(n):
"""Does an unique prime factorization on n.
Returns an ordered tuple of (prime, prime_powers)."""
if n == 1:
return ()
result = []
for p in primes():
(div, mod), power = divmod(n, p), 1
while mod == 0:
if div == 1:
result.append((p, power))
return tuple(result)
n = div
div, mod = divmod(n, p)
if mod != 0:
result.append((p, power))
power += 1
Test run:
>>> un_factor(13195)
((5, 1), (7, 1), (13, 1), (29, 1))
>>> un_factor(600851475143)
((71, 1), (839, 1), (1471, 1), (6857, 1))
>>> un_factor(20)
((2, 2), (5, 1))
EDIT: Minor edits for primes() generator based on this recipe.
EDIT2: Fixed for 20.
EDIT3: Replaced greatest_prime_divisor() with un_factor().
def getLargestFactor(n):
maxFactor = sqrt(n)
lastFactor = n
while n%2 == 0:
n /= 2
lastFactor = 2
for i in xrange(3,int(maxFactor),2 ):
if sqrt(n) < i:
return n
while n%i == 0 and n > 1:
n /= i
lastFactor = i
return lastFactor
This should be fairly efficient. Dividing each factor all out, this way we only find the prime factors. And using the fact that there can only be one prime factor of a number larger than sqrt(n).

Categories

Resources