CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 39

Thread: Prime Numbers

  1. #16
    Join Date
    May 2004
    Posts
    249

    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

  2. #17
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Prime Numbers

    How many primes do you intend to record, and within what range? What kind of analysis do you intend to do?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #18
    Join Date
    May 2004
    Posts
    249

    Re: Prime Numbers

    Quote Originally Posted by laserlight View Post
    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.

  4. #19
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Prime Numbers

    Quote Originally Posted by rockx View Post
    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

  5. #20
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Prime Numbers

    Quote Originally Posted by rockx View Post
    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

  6. #21
    Join Date
    May 2004
    Posts
    249

    Re: Prime Numbers

    I Used Ms Word to open it. but it failed

  7. #22
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  8. #23
    Join Date
    May 2004
    Posts
    249

    Re: Prime Numbers

    so what could i use as my DataType instead of long int.

  9. #24
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Prime Numbers

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #25
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Prime Numbers

    Quote Originally Posted by laserlight View Post
    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.

  11. #26
    Join Date
    May 2009
    Posts
    2,413

    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.

  12. #27
    Join Date
    May 2004
    Posts
    249

    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

  13. #28
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #29
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Prime Numbers

    Quote Originally Posted by laserlight View Post
    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.

  15. #30
    Join Date
    May 2004
    Posts
    249

    Re: Prime Numbers

    Quote Originally Posted by 2kaud View Post
    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:
    Code:
    if (i % j == 0)
    however, is there any way possible to replace 'j' wtih all the numbers that have actually been generated?

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured