so Eri523, what exactly would you suggest.
i dont wanna see the output in the dos window, as i would like to keep a record of the output itself for future analysis and recording
Printable View
so Eri523, what exactly would you suggest.
i dont wanna see the output in the dos window, as i would like to keep a record of the output itself for future analysis and recording
How many primes do you intend to record, and within what range? What kind of analysis do you intend to do?
Get a better editor, one that is designed to open large files.
Windows didn't fail.Quote:
and this failed the windows from opening the file
You didn't mention what you used to open the file. I can open files much larger than 512 MB using editors that are designed to open and manipulate large files. If you're using Notepad, then don't use it, and instead use another editor such as EMEditor or another that allows large file editing.
Regards,
Paul McKenzie
I Used Ms Word to open it. but it failed
Before you decide on the format of the output file from your prime number generation program, you need to know the kind of analysis that is going to be performed on this data and by what method. If you intend to produce even a Trillion prime numbers then you are going to produce very large size files. When dealing with files of this size, the method of access becomes important so the format of the file becomes much more critical to performance issues.
Also, you are going to hit problems with the range of numbers that can be stored within the program variables. Currently the program as posted uses long int (ie signed) so the maximum number that can be stored is 2,147,483,647. Even moving to unsigned long gives a maximum number of 4,294,967,295. So if you want trillions of prime numbers, your program is going to have to move to 64 bit numbers.
so what could i use as my DataType instead of long int.
Maybe unsigned long long, but beware that it might not be available as it is a recent addition to the C++ standard. Or, maybe you need to use an arbitrary precision arithmetic library.Quote:
Originally Posted by rockx
At this point, my take is that you need to do some more experimentation first. Yesterday, you didn't even know that brute force primality testing only needed to be done with possible (prime) factors until the square root of the candidate number. Take your time and experiment with say, finding and recording the first few million primes before you start planning on a billion. Experiment with getting a billion before you start deciding on how to handle a trillion.
Oh, and what analysis do you intend to do? You still have not answered this question.
Just a few suggestions.
Speed; Concurrency has become very straightforward as of C++ 11 so it pays off to make use of all available CPU cores when the primes are calculated.
Big files; Instead of producing one enormous file it's better to make several smaller more tractable files.
Big numbers; There's a GNU library called GMP which allows you to handle arbitrarily big primes,
http://en.wikipedia.org/wiki/GNU_Mul...hmetic_Library
I have tweeked the code a little bit more. And i know it could furthter be improved
And the question on analysis, well nothing tooo big and nothing too serious. and not really analysis, i might keep the record for future references, i dont really know when i might need them. If i get to learn to do something with it in the future then i guess this should be a good start off for meCode:#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
long int i = 1,
c = 3,
n,
s,
j;
int input;
cout << "Enter the Values to be calculated: ";
cin >> input;
ofstream myfile ("PrimeNumbers.doc");
if (myfile.is_open()) {
myfile << 1 << "=\t" << 1 <<"\n\n";
myfile << 2 << "=\t" << 2 <<"\n\n";
while (c <= input) {
n = 0;
i += 2;
s = (long)sqrt((float)i);
for (j = 2; j <= s; j++) {
if (i % j == 0) {
n += 1;
break;
}
}
if (c >= (input-50))
{
myfile << c << "=\t" << i <<"\n\n";
c += 1;
}
else if (n == 0) {
c += 1;
}
}
myfile.close();
} else
cout << "Unable to open file";
return 0;
}
Oh, I think this is the reason why MS Word could not open your file:
My guess is that Word expected your file to be in Microsoft's Word document format, but it was merely a plain text file, hence the error. If you had used say, a ".txt" or ".csv" file extension instead, then perhaps it would have treated the file differently by default.Code:ofstream myfile ("PrimeNumbers.doc");
By the way, I suggest that you don't use tab characters to pretty align your code. Using tab characters for indentation is fine, but alignment with tabs is generally a bad idea because tab widths depend on the editor settings.
I'd have assumed that Word at least checks the file signature to verify it's a valid .doc file, and in case of failure falls back to opening it as plain text. However, after reading your post, I did a real-life check on that with my Word 2003 (newer versions may perhaps behave differently). I renamed a plain text file to .doc and attempted to open it in Word. After dismissing a few error message boxes informing me that Word was unable to start some converter, it opened the file as plain text perfectly fine. Maybe it's simply the OP's file being too large? Note, however, that the file I did the test with didn't contain tabs, which may have influenced the result, due to Word attempting to auto-detect the file format.
I have got another question. the above code simply works fine. but is there any other way possible to further modify this
This particular line does what its supposed to do:
however, is there any way possible to replace 'j' wtih all the numbers that have actually been generated?Code:if (i % j == 0)