using nested for loop, workin with displaying prime numbers

Discussion in 'C++' started by triplejump24, Nov 7, 2006.

  1. triplejump24

    triplejump24 Guest

    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 <iostream>
    # include <cmath>
    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<double>(100)) << endl; //static cast conversts
    from one type to another
    }
    return 0;
    }
     
    triplejump24, Nov 7, 2006
    #1
    1. Advertising

  2. triplejump24

    Daniel T. Guest

    "triplejump24" <> wrote:

    > 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?


    Here's a pointer for you. Start with the code below. When you run it, it
    will *not* print "working so far". Insert code where it says "insert
    code here" until you can get "working so far" to print (It should be
    quite easy :) After you get that part done, un-comment the second
    assert and modify the code until 'working so far" prints again, then the
    next comment, &c.


    #include <iostream>
    #include <cassert>
    // 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.
     
    Daniel T., Nov 7, 2006
    #2
    1. Advertising

  3. > 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<p;n++) in side the loop you can then
    check if n divides p. If it does, p is not a prime.
    If the loop completes without n dividing p, p is a prime.

    If you put all this into a function called IsPrime(int p) which can
    return a bool. ( true if the argument was a primenumber, false if
    not)

    if you now want test a number you can call

    if(IsPrime(number_to_test))
    {
    // This was a prime number
    }

    You can now use a loop to test all the numbers you would like...
    (hint "for loop") or you can write each test explicitly
    if(Isprime(2))
    :
    :
    if(Isprime(34))

    I know there is a lot of room for improvement in my is prime
    function. But start by focussing on getting the function to work
    correctly first, then you can always optimise the code if needed.
    One optimization is only to test integers smaller than or equal to
    the squareroot of p. another ( and more difficult) is to only test a
    against all primenumbers smaller than the square root of p, provided
    you know ALL of them!!

    Two things I think you should consider is what should happen is you
    call IsPrime(0) and IsPrime(1). You wil probably have to modify the
    IsPrime function to handle this in a sensible way.

    A few comments on the code:
    > using namespace std;

    See http://www.parashift.com/c -faq-lite/coding-standards.html
    section 27.5 for a discussion.

    You need to use more comments in your code!
    If you have a varible, you should make a comment why its there, and
    what is is used for. The same applies to functions and files.
    make it a good habbit to write many comments, and most importantly
    keep the comments in sync with the code!

    > cout << sqrt(static_cast<double>(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
     
    =?ISO-8859-15?Q?Bo_M=F8ller?=, Nov 7, 2006
    #3
  4. triplejump24

    Don Guest

    triplejump24 wrote:
    > Hey. Im trying to make program that basically displays all the prime
    > numbers.


    #include <stdio.h>

    /* 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<n); i++)
    r &= _(n,(m<=n)?i*m:0,d-1)|!_(i,1,i);
    return r;
    }

    /*------------------------------------------
    Print primes up to the requested value
    --------------------------------------------*/
    int main(int argc, char* argv[])
    {
    for(int n = 2; n; n++)
    printf("%d is%s prime\n",n, _(n,1,n)?"" : " not");
    return 0;
    }
     
    Don, Nov 10, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SW5kZXB0aA==?=
    Replies:
    1
    Views:
    486
    Bruce Barker
    Apr 1, 2005
  2. nyenyec
    Replies:
    3
    Views:
    1,029
    Duncan Booth
    Jan 7, 2007
  3. Sukeerthi Adiga

    :true not workin!

    Sukeerthi Adiga, Feb 27, 2008, in forum: Ruby
    Replies:
    1
    Views:
    87
    Andreas Kleindiek
    Feb 27, 2008
  4. Jeremy Fischer
    Replies:
    0
    Views:
    212
    Jeremy Fischer
    Jan 16, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    407
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page