Author |
Message |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: 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:
code: | var n := 100000
var timeTaken : int
var primes : array 1 .. n of boolean
primes (1) := false
for i : 1 .. n
primes (i) := true
end for
for i : 2 .. ceil (sqrt (n))
for j : i * 2 .. n by i
primes (j) := false
end for
end for
clock (timeTaken)
put "It took ", (timeTaken / 1000) : 0 : 2, " seconds to calculate primes from 1 to ", n, "."
| [/code] |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Delos
![](http://www.members.shaw.ca/rfolz/delos_avatar.gif)
|
Posted: Tue Apr 25, 2006 7:22 pm Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Tue Apr 25, 2006 8:53 pm Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
Delos
![](http://www.members.shaw.ca/rfolz/delos_avatar.gif)
|
Posted: Tue Apr 25, 2006 10:22 pm Post subject: (No subject) |
|
|
Yup. Listen to zylum, he knows what he's talking about... . |
|
|
|
|
![](images/spacer.gif) |
codemage
![](http://usera.imagecave.com/codemage/codemage-small.gif)
|
Posted: Wed Apr 26, 2006 8:16 am Post subject: (No subject) |
|
|
It's not completely insignificant though.
Bubble and selection sort are both N^2 becuase we drop the lower terms.
Bubble != selection though. |
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Wed Apr 26, 2006 1:00 pm Post subject: (No 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. |
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Wed Apr 26, 2006 1:35 pm Post subject: (No 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? |
|
|
|
|
![](images/spacer.gif) |
Delos
![](http://www.members.shaw.ca/rfolz/delos_avatar.gif)
|
Posted: Wed Apr 26, 2006 4:33 pm Post subject: (No subject) |
|
|
It wouldn't . I misread your code. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Wed Apr 26, 2006 4:46 pm Post subject: (No subject) |
|
|
dam i remember making that for my compsci class i like the way you did it, except i made mine so the user could input the highest number to search to and i kept a counter instead of an array (so i didnt have to initialize it) it worked pretty good, although i wish i had the algorithim you had, mine just checked for all of the factors for it, and if it only had 2 (1 and itself) then it was a prime number and outputted it to the screen |
|
|
|
|
![](images/spacer.gif) |
|