-
May 15th, 2013, 04:12 AM
#16
Re: Prime Numbers
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
-
May 15th, 2013, 04:45 AM
#17
Re: Prime Numbers
How many primes do you intend to record, and within what range? What kind of analysis do you intend to do?
-
May 15th, 2013, 04:52 AM
#18
Re: Prime Numbers
Originally Posted by laserlight
How many primes do you intend to record, and within what range? What kind of analysis do you intend to do?
Well i dont have set limit as yet. but as much s possible. say about 1-2 Trillion.
-
May 15th, 2013, 04:52 AM
#19
Re: Prime Numbers
Originally Posted by rockx
so Eri523, what exactly would you suggest.
Get a better editor, one that is designed to open large files.
and this failed the windows from opening the file
Windows didn't fail.
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
-
May 15th, 2013, 04:55 AM
#20
Re: Prime Numbers
Originally Posted by rockx
Well i dont have set limit as yet. but as much s possible. say about 1-2 Trillion.
So you're going to produce multi-terabyte files? That won't even fit on an entire modern hard drive.
What kind of analysis are you expecting to do on trillions of prime numbers?
Regards,
Paul McKenzie
-
May 15th, 2013, 04:55 AM
#21
Re: Prime Numbers
I Used Ms Word to open it. but it failed
-
May 15th, 2013, 05:20 AM
#22
Re: Prime Numbers
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 15th, 2013, 05:26 AM
#23
Re: Prime Numbers
so what could i use as my DataType instead of long int.
-
May 15th, 2013, 05:58 AM
#24
Re: Prime Numbers
Originally Posted by rockx
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.
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.
-
May 15th, 2013, 08:07 AM
#25
Re: Prime Numbers
Originally Posted by laserlight
Maybe unsigned long long, but beware that it might not be available as it is a recent addition to the C++ standard.
At least VC++ 2010 does support it. (Well, actually, I didn't try with unsigned, but I'm confident that if it works without explicitly specifying signedness, that will work as well.)
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
May 15th, 2013, 09:54 AM
#26
Re: Prime Numbers
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
Last edited by nuzzle; May 15th, 2013 at 12:02 PM.
-
May 15th, 2013, 05:04 PM
#27
Re: Prime Numbers
I have tweeked the code a little bit more. And i know it could furthter be improved
Code:
#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;
}
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 me
-
May 15th, 2013, 09:58 PM
#28
Re: Prime Numbers
Oh, I think this is the reason why MS Word could not open your file:
Code:
ofstream myfile ("PrimeNumbers.doc");
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.
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.
-
May 16th, 2013, 06:23 AM
#29
Re: Prime Numbers
Originally Posted by laserlight
Oh, I think this is the reason why MS Word could not open your file:
Code:
ofstream myfile ("PrimeNumbers.doc");
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.
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 was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
August 14th, 2013, 06:59 PM
#30
Re: Prime Numbers
Originally Posted by 2kaud
Try this. I've kept your code structure but just made a couple of changes. It completes in a few seconds on my computer.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
long int i = 1,
c = 2,
n,
s,
j;
ofstream myfile ("Prime.doc");
if (myfile.is_open()) {
myfile << 1 << "=\t" << 2 <<"\n\n";
while (c <= 150000) {
n = 0;
i += 2;
s = (long)sqrt((float)i);
for (j = 3; j <= s; j+=2) {
if (i % j == 0) {
n += 1;
break;
}
}
if (n == 0) {
myfile << c << "=\t" << i <<"\n\n";
c += 1;
}
}
myfile.close();
} else
cout << "Unable to open file";
return 0;
}
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|