Ok,I have learnt how to generate prime numbers and how to 'split'
input.The above was to help me solve the second 'SPOJ'
challenge,PRIME1.The challenge can be found at
<https://www.spoj.pl/problems/classical/sort=0,start=0> 

I have written my solution and tested it and it works fine,but
unfortunately it is times out when tested by 'SPOJ'.I don't know whether
it times out because it is inherently slow or because a certain
condition takes it into endless loops.I think it is simply because it
needs further optimisation.I have researched as much as I could but
can't find any way to improve the speed of the program (problem with
being a newbie is you don't know what you don't know!)

Could you please look at the program (attached) and advise possible
optimisations or point me to possible resources that would help solve
this challenge.

Thanks,

Boyks 
#!/usr/bin/env python

'''A program to generate prime numbers when given 2 numbers'''

def isPrime(number):
    number=abs(int(number))
    #1 is not considered a prime number
    if number<2:
        return False
    #2 is the only even prime number
    if number==2:
        return True
    #no even prime numbers after 2
    if not number&1:
        return False
    #to find all prime numbers we need to go up to the
    #square root of the highest odd number
    for x in range(3,int(number**0.5)+1,2):
        if number%x==0:
            return False
    return True

def printOutListOfPrimes(startNum,endNum):
    
    #startNum and endNum commented out.Will be collected from user input

    #startNum=int(raw_input())
    #endNum=int(raw_input())
    for num in range(startNum,endNum):
        check=isPrime(num)
        if check==1:
            print num
        

def getNumOfTimes():
    times=int(raw_input())
    return times
    print times

def getValues():
 
    val=[]
    n=getNumOfTimes()
    for r in range(n):
        input=raw_input()
        newInput=input.split()
       # val.append(newInput)
        val+=newInput

    return val        

if __name__ == "__main__":
    
    x=getValues()
    y=0#variable to control the indices
    for i in range(len(x)-1):
        y=0 #to keep track of indices
        printOutListOfPrimes(int(x[y]),int(x[y+1]))
        print
        y+=2

        
         


       


    






    


_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to