Homework 3, The Sieve File.
Short Description:
Write a program that will calculate the prime numbers less than n using
the Sieve of Eratosthenes.
Goals
When you finish this homework, you should have:
- Employed open, read, write, close and lseek.
Formal Description
Write a program that will compute the prime numbers less than n, a given constant. Do this using the
Sieve of Eratosthenes. To employ the techniques discussed in class, DO NOT use arrays but instead use a file.
- Initialize a file of length n (give or take), to a given character. This is a simple write loop. This loop must use the write system call.
- Set current prime to 2
- While the current prime is less than or equal to the square root of n
- Cross off all multiples of the current prime with a different character. This must include at least one
lseek
and write
system calls.
- Starting at the current prime + 1, find the next value not crossed off. Make this the current prime. This must be done with
lseek
and write
.
- Starting again at 2, print out all values that have not been "crossed off".
Discussion
- You must do your computations in a file, not an array, vector, map, list, ...
- You may not use iostreams or c style FILEs. All file I/O must be done via read/write.
- If you wish, you may work at the bit level using &, |, ^, and ~. This way, your file will be $\lceil \frac{n}{8} \rceil$ bytes long instead of n, but your work will be way more complex.
- I will give up to 50% extra credit for this technique. If you do this, please note this in a README file in your submission.
- I did not do this, I think it might not be too hard but ...
- If you wish, you may visualize your result by producing an image file.
- You may choose a value for n which factors nice (200x2000 for example).
- This must all be done as part of your program.
- You may use the "system" system call to execute commands such as cat, cp, ...
- You must use the output of the first part directly, ie add a header or footer to this file, you may not rewrite the file.
- In this portion of your program only, you may use iostream, fstreams, or FILES.
- I made a net pbm file then called pbmtopng to convert it to a usable image file.
- Yes, Chris, you can write a svg file if you wish, the rest of you, can write a jsascript interpreter which will load your file and display it.
- here is my picture.
- See these examples.
- I will give up to 25% extra credit for this visualization. If you do this, please note this in a README file in your submission.
- If you do anything else you believe is worthy of extra credit, note it in a README file in your submission.
Required Files
A single tar file containing the source code and makefile for this program.
Submission
Submit the assignment to the D2L folder Homework 3 by the due date.