Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Prime Numbers
Index -> General Discussion
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
A.J




PostPosted: Mon Jun 04, 2012 2:41 am   Post subject: RE:Prime Numbers

Exactly. As every multiple of i before i*i would have already been crossed out by the time we reached i.
Sponsor
Sponsor
Sponsor
sponsor
Aange10




PostPosted: Mon Jun 04, 2012 2:38 pm   Post subject: RE:Prime Numbers

Whew! Sweet. I love it when things work.

I've now computed the answer in 800 ms with:

Python:

def findPrime (maxNum):
    listOfPrimes = [1] * (maxNum - 1) # Because we exclude 1
    for divBy in xrange(int(floor(sqrt(maxNum)))):
        if listOfPrimes [divBy]:
            # divBy is the index, divBy +2 is the index's value
            for multiples in xrange (((divBy + 2) * (divBy + 2)), maxNum + 1, divBy + 2):
                listOfPrimes [multiples - 2] = 0
    counter = 1
    sum = 0
    for i in listOfPrimes:
        counter += 1
        if i:
            sum += counter
   
    return sum


Thanks you guys Very Happy
A.J




PostPosted: Mon Jun 04, 2012 9:10 pm   Post subject: RE:Prime Numbers

I'm glad it works. It's a great feeling when you see that big green check mark Smile.
Display posts from previous:   
   Index -> General Discussion
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 18 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: