Wednesday, April 12, 2006
CS + AM: Eratosthenes' Sieve
As mentioned earlier, I'm in an essentially Applied Math course through my Computer Science department, dealing with the history and applications of various algorithms.
On Tuesday, my class played (some more) with the Sieve of Eratosthenes, which is a quite interesting way to find prime numbers. There are two ways you can create the sieve. The first of which is starting from the number 2 above your second column. The second way, which is what my class was dealing with, is to only write odd numbers, starting at 1 and going to whatever threshold you'd like to stop at.
In class, we received a 15-column wide sieve, containing the odds from 1 to 1019, inclusive (we wanted to count the primes less than 1020). Now, by the definition in mathematics, 1 is not a prime. I won't explain that, since I don't know the details at the moment (I'll probably look at Wikipedia later for such things). Now, starting from the next number, 3, decide if it is a prime. As 3 is a prime, circle it and begin counting every n squares. Every third square will be divisible by this prime n. Cross out every one of these numbers.
So, after you have 3 circled, you should cross out 9, 15 and such before going down to the next row of numbers. But first, if you are using a number of columns divisible by 3, you should look at the squares directly below those which you have already circled and crossed out. If done correctly, every square in a straight line downwards will be divisible by three. You can cross out the rest of the column if this is the case. There will also be cases where you can have diagonal lines crossing squares out (which I believe happens with 7 on a 15-column sieve).
After you finish crossing out all those numbers divisible by 3, move on to the next number, 5. Five is a prime, so circle it and go to the square containing its square (the square containing n squared, or n^2). Cross this position out, then continue to cross out every nth item. Continue on through 7 and then look at 9. Note that we have already crossed it out, so go straight on to 11. This is a prime, so circle it and continue the process started with 3, 5 and 7. Continue this work until you reach a number whose square (^2) is greater than your last odd number. Once you get to that point, all 'composite' (i.e.- not prime) numbers should be crossed out. Now you can go through the Sieve to count up the number of primes in that set of numbers, then add 1.
The one is added because 2 is a prime, though it is not included in the Sieve because it is even. Interesting algorithm, no? If a current programmer, or have previous experience, think of how you could implement this algorithm in your language of choice (hint: try using a zero over each composite, rather than trying to use a blacklist).
Subscribe to:
Post Comments (Atom)
5 comments:
Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!
[url=http://www.onlinecasinos.gd]casino[/url], also known as accepted casinos or Internet casinos, are online versions of noted ("slab and mortar") casinos. Online casinos ok gamblers to pick up ingredient in and wager on casino games heart of the Internet.
Online casinos superficially embark on aside on the bazaar odds and payback percentages that are comparable to land-based casinos. Some online casinos operation higher payback percentages during hollow thingumabob games, and some punt back exposed payout magnitude audits on their websites. Assuming that the online casino is using an fittingly programmed unsystematic multitudinous generator, note games like blackjack clothed an established congress edge. The payout thin up objective of these games are established at just about the rules of the game.
Dissimilar online casinos license in give someone the run-around b cajole a cargo of or face their software from companies like Microgaming, Realtime Gaming, Playtech, Supranational Trick Technology and CryptoLogic Inc.
top [url=http://www.c-online-casino.co.uk/]online casinos[/url] check the latest [url=http://www.realcazinoz.com/]free casino[/url] autonomous no deposit reward at the leading [url=http://www.baywatchcasino.com/]casino online
[/url].
hiya and welcome washii.blogspot.com blogger discovered your website via yahoo but it was hard to find and I see you could have more visitors because there are not so many comments yet. I have discovered site which offer to dramatically increase traffic to your site http://mass-backlinks.com they claim they managed to get close to 4000 visitors/day using their services you could also get lot more targeted traffic from search engines as you have now. I used their services and got significantly more visitors to my website. Hope this helps :) They offer best services to increase website traffic at this website http://mass-backlinks.com
Post a Comment