
August 14th, 2013, 09:41 PM
#31
Re: Prime Numbers
Originally Posted by rockx
I have got another question. the above code simply works fine. but is there any other way possible to further modify this
Of course there is. Prime number generation is one of the mostresearched subjects in computer sciences and mathematics, so there's plenty of other variations out there to study.
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?
What are you asking for? j already is being replaced with all candidate numbers to check for being an integral divider, timesequentially within the loop.
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 15th, 2013, 11:29 PM
#32
Re: Prime Numbers
At the moment j consists of all integers, from 2  sqrt(i). This is including all the calculated prime numbers as well as the composite numbers. So i m asking if j could be replaced with only the prime numbers so all the composite numbers between 2 and sqrt(i) is eliminated.

August 16th, 2013, 05:11 AM
#33
Re: Prime Numbers
In my original code, the j loop has bounds of 3 and sqrt(i) and increments by 2 to try every odd number.
A prime number is not prime if it is divisible by a prime. The code could be modified so that when a prime is found it is added to a set container and the j loop could be replaced by an iteration of the set. Thus you would only be trying prime numbers and hence eliminate all the composite 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++17 Compiler: Microsoft VS2017 (15.6.6)

August 16th, 2013, 06:08 AM
#34
Re: Prime Numbers
Originally Posted by rockx
At the moment j consists of all integers, from 2  sqrt(i). This is including all the calculated prime numbers as well as the composite numbers. So i m asking if j could be replaced with only the prime numbers so all the composite numbers between 2 and sqrt(i) is eliminated.
Well, the elimination process you mention is just what your prime number code performs, i.e. what it's there for. If j would enumerate just the primes in the first place (maybe provided by some standard library algorithm or the like), there would be no need for your own prime number program at all.
Or, perhaps, what you're really asking for is a quantum computer...
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 16th, 2013, 02:46 PM
#35
Re: Prime Numbers
Originally Posted by Eri523
Well, the elimination process you mention is just what your prime number code performs, i.e. what it's there for. If j would enumerate just the primes in the first place (maybe provided by some standard library algorithm or the like), there would be no need for your own prime number program at all.
As I understood the suggestion, you can iterate through the list of ALREADY found prime numbers, than switch to the odd numbers above the last value.
Vlad  MS MVP [2007  2012]  www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows  replacement windows manager for Visual Studio, and more...

August 16th, 2013, 04:24 PM
#36
Re: Prime Numbers
Originally Posted by 2kaud
In my original code, the j loop has bounds of 3 and sqrt(i) and increments by 2 to try every odd number.
A prime number is not prime if it is divisible by a prime. The code could be modified so that when a prime is found it is added to a set container and the j loop could be replaced by an iteration of the set. Thus you would only be trying prime numbers and hence eliminate all the composite numbers.
Try this
Code:
#include <fstream>
#include <iostream>
#include <cmath>
#include <set>
using namespace std;
const char pfnam[] = "Prime.doc";
typedef unsigned long int ULINT;
typedef set<ULINT> setprimes;
typedef setprimes::const_iterator pci;
const ULINT maxprime = 150000;
int main()
{
ULINT pno = 0;
setprimes primes;
ofstream myfile (pfnam);
if (!myfile.is_open()) {
cout << "Unable to open file " << pfnam << endl;
return 1;
}
myfile << ++pno << "=\t" << 2 <<"\n";
for (ULINT c = 3; c <= maxprime; c+=2) {
ULINT s = (ULINT)sqrt((long double)c);
bool notp = false;
for (pci pn = primes.begin(); pn != primes.end() && *pn <= s && !notp; ++pn) {
notp = (c % *pn == 0);
}
if (!notp) {
primes.insert(c);
myfile << ++pno << "=\t" << c <<"\n";
}
}
myfile.close();
return 0;
}
Note that this method is memory intensive as the set primes holds all the previously found primes and when the outer c loop terminates, will contain all found primes.
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++17 Compiler: Microsoft VS2017 (15.6.6)

August 16th, 2013, 05:07 PM
#37
Re: Prime Numbers
Originally Posted by VladimirF
As I understood the suggestion, you can iterate through the list of ALREADY found prime numbers, than switch to the odd numbers above the last value.
Ah, ok, in this case I in deed misunderstood the question. Of course the primes calculated so far can be used to test for divisibility rather than each odd number up to the sqrt. And, given the program is to calculate the primes sequentially without skipping some (as all the proposals so far in this thread have been doing IIRC), falling back to odds instead of already calculated primes isn't even necessary: The overall prime density is about equal (can't tell the mathematical proof for that off the top of my head, though), so running out of already calculated primes in't expected to happen.
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 16th, 2013, 09:55 PM
#38
Re: Prime Numbers
Originally Posted by Eri523
Well, the elimination process you mention is just what your prime number code performs, i.e. what it's there for. If j would enumerate just the primes in the first place (maybe provided by some standard library algorithm or the like), there would be no need for your own prime number program at all.
Or, perhaps, what you're really asking for is a quantum computer...
Please let me rephrase my question. COuld it be possible for j to contain only the calculated Prime Numbers, no other numbers apart from the calculated numbers?

August 16th, 2013, 10:06 PM
#39
Re: Prime Numbers
Originally Posted by rockx
Please let me rephrase my question. COuld it be possible for j to contain only the calculated Prime Numbers, no other numbers apart from the calculated numbers?
Yes. See 2kaud's post #36. The variable in question isn't named j there, though.
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.
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
OnDemand Webinars (sponsored)
