1

to gain hands-on experience, I'm trying to solve the problems in spoj . The problem in the link asks to find all prime numbers between given 2 numbers. So how I implement this with python 2.7

# printing all prime numbers between given two inputs
import math
def findPrimes(num1,num2):
    for num in range(num1,num2+1):
        isPrime=True
        for i in range(2,int(math.sqrt(num))+1):
            if num%i==0:
                isPrime=False
                break
        if isPrime:
            print num


def main():
    inputs=[]
    numOfTestCases=int(raw_input())

    while(numOfTestCases>0):
        line=raw_input()
        numbers=line.split()
        inputs.append(numbers)
        numOfTestCases-=1

    for testCase in inputs:
        findPrimes(int(testCase[0]),int(testCase[1]))
        print ""

main()

However, when I send the code, I get time-exceed limit. How could I make my code fast enough?

DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
zwlayer
  • 1,752
  • 1
  • 18
  • 41
  • 2
    I'd recommend looking at the Sieve of Eratosthenes. Assuming you aren't producing huge primes, it should be sufficient for your purposes. – Nathan Merrill Nov 27 '15 at 21:18
  • @NathanMerrill actually, there are really huge numbers in the test cases – zwlayer Nov 27 '15 at 21:19
  • 2
    This is more of a maths algorithm questions than code - there are a number of known solutions algorithmically to this with differing complexities. – Andrew Nov 27 '15 at 21:21
  • use the sieve of eratosthenes, and I'd say hard code prime numbers upto some number to make it faster still. also choose `pypy` as compiler if it's there – Ayush Nov 27 '15 at 21:23
  • @Andrew, although I agree with you, I also think that, the question contains an algorithmic point of view even if it is related with math – zwlayer Nov 27 '15 at 21:23
  • @zwlayer Just looked at the question, it has a tag indicating Sieve of Eratosthenes. It's definitely the algorithm you want. – Nathan Merrill Nov 27 '15 at 21:24
  • 1
    @zwlayer - the maths stack exchange site would be more suitable in effect. – Andrew Nov 27 '15 at 21:24
  • @NathanMerrill thank you for the hint. This is my first time at Spoj, so I couldnt see that tag. – zwlayer Nov 27 '15 at 21:25
  • http://thelivingpearl.com/2013/01/06/how-to-find-prime-numbers-in-python/ – palsch Nov 27 '15 at 21:41

2 Answers2

1

You should use the Sieve of Eratosthenes and it is quite simple. First you initialize all numbers to be prime. Then for each prime you remove its multiples from the prime list. And it's time complexity is near liner O(nloglogn). Something like this:

N = 1000
is_prime = [1]*N
for i in xrange(2,N):
    if is_prime[i]:
        for j in xrange(2*i,N,i):
            is_prime[j] = 0

This implementation should do just fine. But there are some extra optimizations that you can find them in the link above.
Note that 0 and 1 are not prime.

Community
  • 1
  • 1
Saeid
  • 4,147
  • 7
  • 27
  • 43
0

No, the numbers aren't huge in spoj/PRIME1. The sieve of Eratosthenes works extremely well there, but even trial division gets you through there, if you test by primes, and test odds only (or better, only 6-coprimes or 30-coprimes).

You need only find primes below the square root of your top limit, in advance. sqrt(10^9) is about 32,000, so there are only about 3,400 primes to maintain. That's nothing.


6-coprimes: numbers coprime with 6, i.e. with 2 and 3, so there's no need to test divide them by 2 nor 3, when testing for primes. You need to find a way to generate them directly, so there won't be any multiples of 2 and 3 among the numbers you need to test, by construction.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • something still not clear? do ask. otherwise, please mark as accepted the one answer that was most helpful to you. :) – Will Ness Dec 15 '15 at 21:54