Unless you are developing code for Borland, get rid of that copyright at the top of the file.
You own it. Not Borland. (It is
very dishonest for them to stick that in there.)
Please use [
code] tags. If your code looks like it does here when you are editing it, you will never figure out what is wrong. The way you
physically organize your code has a
causal effect on the quality of the code and your understanding of it.
In the following example, I've simply added some proper whitespace to your prime() function (which is otherwise very nice, if simple):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
long prime(long n)
{
long l;
if (n % 2 == 0) return (n==2);
if (n % 3 == 0) return (n==3);
if (n % 5 == 0) return (n==5);
for (l=7; l*l <= n; l+=2)
if (n % l == 0)
return 0;
return 1;
}
|
More on prime() later...
By the way,
use good variable names. By that I mean that your variable names should tell you something about the variable's meaning. I will spare the lecture here, but every programmer should be made to read and understand the document:
Ottinger's Rules For Variable and Class Naming
http://www.objectmentor.com/resources/articles/naming.htm
In particular,
never use 'l' and 'O' for variable names. (That's a lowercase 'L' and an uppercase 'o'.)
A good throw-away name here might be
i or
x, since these are common counter names. A better name would be something like
divisor or
factor.
-------------------------------------------------------------------------------------------------
Ok, enough of that stuff. On to your actual question. You wish to save the list of numbers to file. To do that, use
fprintf() instead of
printf().
1 2 3
|
for (n=2; n<=max_n; n++)
if (prime(n))
fprintf(primes_file, "%ld ", n);
|
(Notice how I renamed some stuff.)
-------------------------------------------------------------------------------------------------
That
flush() function is completely unnecessary. Don't second-guess the standard library or the operating system. If you really want unbuffered output, use
setbuf( primes_file, NULL );
. That is really a bad idea though. Leave the buffering to the default and you'll be fine. I recommend that you output a newline instead of a space after every prime number, and set your buffering to line-buffered:
1 2 3
|
setvbuf(primes_file, NULL, _IOLBF, 1024);
...
fprintf(primes_file, "%ld\n", n);
|
Doing so will greatly reduce the likelihood of a corrupt output (should your program be unceremoneously killed, or crash).
-------------------------------------------------------------------------------------------------
You can save your numbers as
binary if you like, but it introduces the effort of handling binary file I/O --which is not as simple as text file I/O, particularly when handling the minimum required size for each output datum. If you are concerned about space, then it is probably a good idea. Otherwise it isn't worth the effort.
-------------------------------------------------------------------------------------------------
If you really plan to print a lot of prime numbers, you will need to improve your prime number routine. I suggest you check-out the Strong Compositeness Test to determine potential primality (it is the simplest method, using Fermat's Little Theorem), and only if the test indicates that you have a potential prime should you spend additional time verifying it is not a Carmichael number (which is not a prime). Prime testing gets slow, fast.
There are more sophisticated methods, such as the Rabin-Miller test, which is also probablistic, but is probably the fastest you will find. It is also relatively easy to turn into a deterministic test. For more on primality testing, see the Wikipedia
http://en.wikipedia.org/wiki/Primality_test
-------------------------------------------------------------------------------------------------
You may also want to find yourself a
bignum library. The following is a good one:
GNU MP Bignum Library
http://gmplib.org/
It is LGPL so you can use it without being obligated to release your source, should you desire to distribute only binaries of your application. (If you don't plan to distribute anything at all, then licensing is a moot point.)
-------------------------------------------------------------------------------------------------
One thing I think you wondered is how to pick-up at the last-output prime number and continue from there. I would make a little function to do it. Something like:
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
|
/*
* prepare_to_write()
*
* function
* Positions the write pointer in the file such that it is ready to
* receive more prime numbers.
*
* arguments
* f -- The file
*
* returns
* A bignum: The last complete prime number listed in the file.
*
* If there aren't any prime numbers in the file (for example, if the
* file was just created prior to calling this function), returns zero.
*/
mpz_t prepare_to_write( FILE* f )
{
On error: return 0
1. seek to the end of the file
2. move backwards until you come to the beginning of a number
3. remember the file position
4. repeat step 2
5. read the last two numbers from the file
6. if (mpz_cmp( penultimate_number, last_number ) > 0)
result <-- penultimate_number
seek to file position remembered in step 3
else
result <-- last_number
seek to end of file
7. return the result
}
|
Whew. Hope this helps.