Computer Science Canada Sieve of Eratosthenes |
Author: | MysticVegeta [ Tue Apr 25, 2006 6:10 pm ] | ||
Post subject: | Sieve of Eratosthenes | ||
Remember in middle school when we had to calculate methods by writing all the numbers down and incrementing the divisor and crossing out all of its multiples until we reach the number? Well a same concept has been implemented into this algorithm to calculates primes really fast! so now there is no way your program can timeout in contests even with Turing!!!! Pretty self explanatory code:
|
Author: | Delos [ Tue Apr 25, 2006 7:22 pm ] |
Post subject: | |
Mystic, you've been around here long enough to know that we've seen this posted at least eleventy billion times [/SNL reference]. I would like to point out that you should optimize your code just a little. That first for loop (initializing all to true) is unnecassary. Incorporate that into an if statement in your actual check - will cut down your number of computations by n - which can be a lot at lower values. |
Author: | zylum [ Tue Apr 25, 2006 8:53 pm ] |
Post subject: | |
cutting the algorithm by N is insignificant since the entire algo is O(N^2). If you've ever done algorithm complexity youd learn that you only keep the term with highest degree. besides, i dont see how it will work without initializing the array. |
Author: | Delos [ Tue Apr 25, 2006 10:22 pm ] |
Post subject: | |
Yup. Listen to zylum, he knows what he's talking about... ![]() |
Author: | codemage [ Wed Apr 26, 2006 8:16 am ] |
Post subject: | |
It's not completely insignificant though. Bubble and selection sort are both N^2 becuase we drop the lower terms. Bubble != selection though. |
Author: | zylum [ Wed Apr 26, 2006 1:00 pm ] |
Post subject: | |
it is almost completely insignificant, especially for larger values of N. btw, time complexity isnt necessarily to see how fast your algo is, it is more to determine how much slower it will be for an increase of input. so you know an N^2 algo will be about 4 times slower for twice as much input. even if you have that extra loop that adds N to the time, over all, again especially with larger Ns, that extra N will be almost insignificant. |
Author: | MysticVegeta [ Wed Apr 26, 2006 1:35 pm ] |
Post subject: | |
It has been posted before? damn I must have missed it, I just thought it was cool and better than creating a function and running a loop from 2 to sqrt(N) everytime.. Anyways, how would it work without intializing the array? |
Author: | Delos [ Wed Apr 26, 2006 4:33 pm ] |
Post subject: | |
It wouldn't ![]() |
Author: | Clayton [ Wed Apr 26, 2006 4:46 pm ] |
Post subject: | |
dam i remember making that for my compsci class ![]() ![]() |