-
Re: Prime Numbers Help?
Ok, guys. That's some stuff I haven't heard about yet. Getting back to my problem, the following code (I hope it's better now, at least esthetically) generates the following output. It does not display the same numbers a billion times, but it skips all the divisors except 2, which is the initial value of N. So the second loop is not working, most probably because of the break keyword, but without it, I get all the numbers from 2 to limit.
Code:
#include<iostream>
using namespace std;
int main() {
int limit;
cout<<"Please enter an interval limit: ";
cin>>limit;
for(int i=1; i<=limit; i+=2) {
for(int n=2; n<limit; n+=2) {
if(i%n!=0) {
cout<<i<<" ";
}
break;
}
}
return 0;
}
OUTPUT:
Code:
Please enter an interval limit: 50
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Process returned 0 (0x0) execution time : 2.453 s
Press any key to continue.
-
Re: Prime Numbers Help?
it is wrong:
9, 15,21 and ... are not prime.
-
Re: Prime Numbers Help?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int i, j;
for ( i = 2; i < 100; i++ ) {
for ( j = 2; j <= i/2; j++ ) {
if ( ! ( i % j ) ) break;
}
if ( j > i / 2 ) cout << i << endl;
}
return 0;
}
This code works great, but can someone help explain this loop to me? I'm especially having a hard time with the if ( ! ( i % j ) ) break; statement.
-
Re: Prime Numbers Help?
This is a (not very) shorthand way of saying
It relies on a non-zero being interpreted as 'true' and zero as 'false'. The '!' inverts the logic.
Therefore, if the value of i % j is zero, then that is interpreted as 'false'. the '!' inverts this to 'true'.
I find the second example preferable as it states the test explicitly.
-
Re: Prime Numbers Help?
just to add more verboseness, are you aware that false is '0' and true is 'not zero'?
so if x%y == 0, then that is equivalent (with respect to bools) to x%y == false, otherwise it is true.
just fyi.
-
Re: Prime Numbers Help?
Hey thanks Amleto for the help, it took me a little while to wrap my brain around the loop sequence, and after I put in cout << "i = " << i << " j = " << j << endl; after the second loop, I could see what it was doing with i and j.
-
Re: Prime Numbers Help?
I know this problem has been solved, but for anyone else who is interested. I has been shown that all prime numbers reside around multiples of 6. So you could slightly speed up the app by adding +=6 instead of one or two. See the code below.
Code:
#include <vector>
#include <iostream>
#include <iomanip>
using namespace std;
// playing with primes
int main() {
vector<int> actualPrimes;
actualPrimes.push_back(2);
actualPrimes.push_back(3);
for( int i=6; i<1093/*arbitrary*/; i+=6 ) {
bool left=true,right=true;
int endSize = actualPrimes.size(),
endDelim = i/2;
for(int j=0; j<endSize && actualPrimes[j]<endDelim; ++j) {
if( (i-1) % actualPrimes[j] == 0 ) {
left = false;
} if( (i+1) % actualPrimes[j] == 0 ){
right = false;
}
}
if( left ) {
actualPrimes.push_back(i-1);
}if(right) {
actualPrimes.push_back(i+1);
}
}
cout<<"The list of primes are:";
for( int i=0; i<actualPrimes.size(); ++i) {
if( i%6 == 0 ) cout<<endl;
cout<<setw(10)<<actualPrimes[i]<<" ";
}
return 0;
}
I was interested in this, so I spat out the code in 5 minutes.
All the best!