Tag Archives: algorithm

Sieve of Eratosthenes

Prime numbers are an important concept in Number Theory and Cryptography which often uses the difficulty of finding prime numbers as a basis for building encryption systems that are difficult to break without going through all (or a very large number of) possible choices.

Remember that a prime number is a number greater than 1 whose only divisors are 1 and that number itself. One of the most famous algorithms for searching for prime numbers is the Sieve of Eratosthenes. I added a script which implements the Sieve of Eratosthenes to my Examples page.

This algorithm prints out all prime numbers less than a given number by first canceling out all multiples of 2 (the smallest prime), then all multiples of 3 (the second smallest prime), then all multiples of 5 (the third smallest prime – multiples of 4 do not need to be considered because they are also multiples of 2), etc until we have reached a number which cannot be a divisor of this maximum number.

So if we are given a number, n, the first step of the algorithm is write out a table that lists all the numbers that are less than n. For example lets run this Sieve on 50. So all numbers less than 50 are

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50.

So since 1 is not a prime number (by the definition of prime numbers), we cancel that number out.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50.

Next, we look at the list and the first number that is not crossed out is a prime. That number is 2. We will put a mark by 2 and cancel out all of 2’s multiples.

1, 2*, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50.

Again, we look at the list and the first number that is not marked or crossed out is 3, so that number is prime. We will put a mark by 3 and cancel out all of 3’s multiples.

1, 2*, 3*, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50.

Once again, we look at the list and the first number that is not marked or crossed out is 5, so that number is prime. We will put a mark by 5 and cancel out all of 5’s multiples.

1, 2*, 3*, 4, 5*, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50.

We look at the list and the first number that is not marked or crossed out is 7, so that number is prime. We will put a mark by 7 and cancel out all of 7’s multiples.

1, 2*, 3*, 4, 5*, 6, 7*, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50.

Now, we look and the first number that is not crossed out is 11. However, since 11 is greater than sqrt(50) we know that each of 11’s multiples that are less than 50 will have been cancelled out by a previous prime number. So we have finished the algorithm.

Check out my script which implements the Sieve of Eratosthenes for more examples.

Examples Page

Sometimes, the most effective way to understand a new concept is to actually see it in action. On My Examples Page, I have implemented a variety of scripts to help teach many different concepts.This is the page that is the easiest for me to update, so you will regularly see changes to this page along with an accompanying blog entry at My Blog Page.

This page is focused on teaching individual concepts and/or algorithms. In particular, with the HTML5 Canvas element, I’ve been able to visualize many of these concepts. Generally I try to provide a script that executes the given algorithm (or concept) and allows for users to view these concepts on random instances. When possible, I provide a button labeled “New Problem” (or something similar) which will allow the user to view a different instance of the algorithm.

Flash Cards Page

One of the most effective ways I remember studying for vocabulary quizzes in high school was through flash cards. During my time in college, I wondered why a similar method was not used to help study for other things, particularly in math and math-like classes where new definitions and concepts are introduced everyday. As a result of this, I wrote an initial flash cards script to help me with my advanced calculus class. Because of my success in this class, I continued developing flash cards to help study many other concepts. Here,  I have decided to share some of those concepts with you.

The general concept behind how I develop the flash cards is simple. Each flash card consists of a question on one side and the answer to that question on the other. For definitions, the question is generally of the form “What is the definition of _____” and the answer will generally go “The definition of _____ is _____”. This may vary a bit, but I try to keep it in this context to allow for searching easier. The other things I generally put onto flash cards are Lemmas/Theorems/Corollaries. These are a bit more difficult to put on flash cards though. The flash cards are still in a question and answer format, but I tried to make the question center around what the question being asked that led to the development of the Lemma/Theorem/Corollary was. Sometimes I may be off-base with these questions, and I’ve had to go back and re-do these questions a number of times. However, I hope they can be helpful.

For algorithms, which are a large part of this site, I generally asked to include the pseudocode for the given algorithm. However since pseudocode is far from formal, this can lead to a bit of a debate among whether you feel that your answer is the same as mine.

The AMS Graduate Student Blog lists some other sites where you can get math generated flash cards.

Hello, World!

The ideas for this site have been bouncing around inside my head and on my computer for years, and this site is an acknowledgement that it was time to finally act on these ideas.

The site will feature a collection of scripts I have written to help illustrate different concepts. A large part of this will be a flash cards section which will provide an avenue to study or to refresh one’s memory on various subjects. I recognize, though, that all subjects are not easily understood through flash cards and so I also have an examples section where various algorithms are implemented on example problems (problem sets) to provide users with a more hands on experience.

The site will be updated regularly, generally with either new subject areas added to the flash cards database, new scripts added to the examples. I leave open the possibility, though, of entirely new sections being introduced as ideas continue to develop and the site continues to grow.

EDIT:
There are several areas that I would like to take the site, but with each area I have the problems of (a) showing the concept, (b) visualizing the concept, c) showing the intuition behind the concept. Sometimes, I will think of (what I call) more clever ways to teach or learn an idea I’ve already discussed. This may lead to more than one page on the same concept. I encourage you to try all such pages to see if you like any of them.

I hope you enjoy.