31.08.2015 Views

Objectives Experiment #1 Introduction to Algorithms

Lab 1 - Rabie Ramadan

Lab 1 - Rabie Ramadan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Prime Numbers:<br />

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors<br />

other than 1 and itself. One of the most common problems is finding the Prime numbers in a<br />

certain range, this can be done using:<br />

1. Brute Force algorithm:<br />

By testing all numbers one by one.<br />

2. Sieve of Era<strong>to</strong>sthenes<br />

Algorithm:<br />

To find all the prime numbers less than or equal <strong>to</strong> a given integer n by Sieve method:<br />

Requirements<br />

1. Create a list of consecutive integers from 2 <strong>to</strong> n: (2, 3, 4, ..., n).<br />

2. Initially, let p equal 2, the first prime number.<br />

3. Starting from p, count up in increments of p and mark each of these numbers greater<br />

than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them<br />

may have already been marked.<br />

4. Find the first number greater than p in the list that is not marked.If there was no such<br />

number, s<strong>to</strong>p. Otherwise, let p now equal this number (which is the next prime), and<br />

repeat from step 3.<br />

When the algorithm terminates, all the numbers in the list that are not marked are<br />

prime.<br />

1- Implement the two algorithms described above.<br />

2- Run both algorithms on the following data<br />

a. 100<br />

b. 1000<br />

c. 10000<br />

d. 100000<br />

e. 200000<br />

3- For the previous data fill the following table.<br />

input Brute Force Sieve<br />

Time in MS Time in MS<br />

100<br />

1000<br />

10000<br />

100000<br />

200000<br />

Winter 2012 <strong>Experiment</strong> <strong>#1</strong> 3/5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!