Assgn4_Part2 - C SC 230 Summer 2011 Assignment 4 Due...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
C SC 230 – Summer 2011 – Assignment 4 Due Thursday July 28 by 4:00 in the course box in ECS. Total Marks = 100 PART 2: PROGRAMMING IN ARM AND C [50 marks] The problem to be solved: the Sieve of Eratosthenes In order to generate all prime numbers up to 1,000,000, the “Sieve of Eratosthenes” is quite an efficient al- gorithm. Make sure you do a web search, as there are examples and interesting explanations for it. The algo- rithm filters the prime numbers by deleting from a list all multiples of those numbers that have already been discovered to be primes. For example, from a list from 1 to N, one would keep 2, but reject 4, 6, 8, etc. as they are all multiples of the initial seed “2”. Similarly one would keep 3 and reject 6, 9, 12, etc. The pseudo-code in Figure 1 defines the solution you need to program, for a case where the maximum range of integers is 0 to 100 and one wants to find all prime number up to a MAX of 50. Define MAXLIST = 100 Declare MAX = 50 (this can change) Declare sieve[MAXLIST] integers Declare primes[MAXLIST] integers Initialization step: int i; sieve[0] = 0; sieve[1] = 0; k = 0; for (i=2, i<MAX; i++) sieve[i] = 1; Find primes: int i,j; for (i=2; i<MAX; i++) { if sieve[i] = 1 { primes[k++] = i for (j=i*2; j<MAX; j+=i) sieve[j] = 0; } } Print primes: for (i=0; i<number of primes k;i++) print list of primes at 15 per line Figure 1: Pseudo code for Sieve The output of your program should be the list of prime numbers you found. Test your program by listing all the prime numbers less than 50 as stored them in an array called “primes” (let the maximum size ever be MAXLIST = 100). Simply print out “primes” to stdout in a pleasing and professional format. Test your program at first by using a shorter list. For example, if MAX (as from Figure 1) were given to be 15, then the output should be: Prime numbers up to 15 are: 2 3 5 7 11 13
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Assgn4_Part2 - C SC 230 Summer 2011 Assignment 4 Due...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online