446,407 Members | 920 Online Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

# using nested for loop, workin with displaying prime numbers

 P: n/a Hey. Im trying to make program that basically displays all the prime numbers. I need to use bool and for but im not quite sure if i have this right. So far i have this bunch of a mess but can anyone point me in the right direction? Thanks! # include # include using namespace std; int i; //int sqrt; //int j; int x; int j; int limit; bool primeNumber(int); int main() { // cout << "Limit: "; // cin >x; // if ((x % 2 == 0) & (x % x == 0)) // { // return true; // } // else // { // return false; // return 0; //} // //bool primeNumber; { // if ((x % 2 == 0) & (x % x == 0)) // { // return true; // } // else // { // return false; // } //} for (i = 2; i <= x; i++) cout << i; { for (j = 2; j <= sqrt(i); i++) cout << sqrt(100.0) << endl; cout << sqrt(static_cast(100)) << endl; //static cast conversts from one type to another } return 0; } Nov 7 '06 #1
3 Replies

 P: n/a "triplejump24" #include // other includes as necessary bool is_prime( int number ) { bool result = false; // insert code here return result; } int main() { assert( is_prime( 2 ) ); //assert( is_prime( 3 ) ); //assert( ! is_prime( 4 ) ); //assert( is_prime( 5 ) ); cout << "Working so far!"; } -- To send me email, put "sheltie" in the subject. Nov 7 '06 #2

 P: n/a Hey. Im trying to make program that basically displays all the prime numbers. I need to use bool and for but im not quite sure if i have this right. So far i have this bunch of a mess but can anyone point me in the right direction? Thanks! First ask yourself what is a primenumber? "An integer p is called a prime number if the only positive integers that divide p are 1 and p itself." So you need to check your would-be primenumber against all other integers. Well you do not need to check againts all other integers. Any integer larger than p will not divide p! So we need to check all integers smaller than p (excluding 0 and 1). This is something you should definitely use a loop for. You can use a "while()" loop, but i would prefer a "for()" loop. something like for(int n= 2; n(100)) << endl; Using a cast operator is something that should make you think "Is this really right?". Casting is often a consecuence of a flawed design. In some cases there are needed, but avoid them as much as possible. I hope this get you closer to your program. Bo Møller -- Bo Møller Hobby-Programmer Nov 7 '06 #3

 P: n/a triplejump24 wrote: Hey. Im trying to make program that basically displays all the prime numbers. #include /* This program implements a blindingly fast O(n^n) algorithm to find prime numbers, using an elegant recursive method. As required it uses bool and for. */ bool _(int n, int m, int d) { bool r = m != n; for(int i=0; d && (i

### This discussion thread is closed

Replies have been disabled for this discussion. 