0
\$\begingroup\$

Below code is for Given an even number (greater than 2), return two prime numbers whose sum will be equal to the given number my task to reach the lowest time complexity I tried it differently, but I cannot reach the lowest time complexity - please help, and I am running it on an online compiler.

class Solution:
    def primesum(self, A):
        # write your method here
        n = A 
        return self.findPrimePair(A)
        
    def findPrimePair(self,n): 
        isPrime = [0] * (n+1) 
        isPrime = [True for i in range(n + 1)] 
        self.SieveOfEratosthenes(n, isPrime)
        for i in range(0,n): 
            if (isPrime[i] and isPrime[n - i]): 
                return i,n-i
        return 0,0
    def SieveOfEratosthenes(self,n, isPrime):
        isPrime[0] = isPrime[1] = False
        p=2
        while(p*p <= n): 
            if (isPrime[p] == True): 
                i = p*p 
                while(i <= n): 
                    isPrime[i] = False
                    i += p 
            p += 1
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You have no detail at all. We have no idea what your code is supposed to do, no idea what the time constraints are, or what your project is. I will be forced to give a -1 (unless you edit it, that is) \$\endgroup\$
    – fartgeek
    Commented Dec 4, 2020 at 2:14
  • \$\begingroup\$ Sorry, I am a beginner; I am learning now. I tried my best to give the details(i edited the code). \$\endgroup\$ Commented Dec 4, 2020 at 2:53
  • \$\begingroup\$ Welcome to Code Review! The original title, which stated your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. I've edited it to help you; please check that I haven't misrepresented your code! \$\endgroup\$ Commented Dec 9, 2020 at 13:24

1 Answer 1

2
\$\begingroup\$
def primesum(self, A):
    # write your method here
    n = A 
    return self.findPrimePair(A)

The variable n is never used, so delete that line. And the comment is obsolete.


    isPrime = [0] * (n+1) 
    isPrime = [True for i in range(n + 1)]

Why not simply initialise to all true? I.e.

    isPrime = [True] * (n+1) 

    self.SieveOfEratosthenes(n, isPrime)
    for i in range(0,n): 
        if (isPrime[i] and isPrime[n - i]): 
            return i,n-i

If we arrange for SieveOfEratosthenes() to return a set, then we don't need to test every single number:

primes = generate_primes(n)
for i in primes:
    if n-i in primes: return i, n-i

We can probably reduce the work further: consider using an ordered list, and traversing it from both ends (or, equivalently, traversing the list of primes and its reverse), advancing one or other iterator according to whether the sum is under or over target, returning when we hit an exact match or the iterators meet.


The sieve itself is doing more work than it needs to. For example, after removing all the even numbers, we can advance by 2*p each iteration, since every other value would be even.


Style-wise, I see a lot of unnecessary parentheses and some redundant comparisons. For example, this line has both:

        if (isPrime[p] == True): 

We would normally write that simply as

        if isPrime[p]:
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.