Little help on small problem

This is a discussion on Little help on small problem within the C++ Programming forums, part of the General Programming Boards category; What I'm trying to do is have a user enter a number have the computer check to see if it ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    4

    Little help on small problem

    What I'm trying to do is have a user enter a number
    have the computer check to see if it is prime number. AND if it is not prime what is the next the next closest prime number. i got the figuring out the prime number part but don't know how to add in the additional info needed (the next closest prime number). Here is what I got so far

    if ( n%2 == 0 ) {
    # The number is even, so not prime.
    }
    else {
    for ( i = 3; i < sqrt(n); 2++ ) { # Check odd divisors only
    if ( n%i == 0 ) {
    # The number is not prime.
    }
    }
    }
    # The number is prime.
    Any ideas
    Thanks
    Matt

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Code tags please.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    4
    What I'm trying to do is have a user enter a number
    have the computer check to see if it is prime number. AND if it is not prime what is the next the next closest prime number. i got the figuring out the prime number part but don't know how to add in the additional info needed (the next closest prime number). Here is what I got so far
    Code:
    if ( n%2 == 0 ) { 
    # The number is even, so not prime.
    }
    else {
    for ( i = 3; i < sqrt(n); 2++ ) { # Check odd divisors only
    if ( n%i == 0 ) {
    # The number is not prime.
    }
    }
    }
    # The number is prime.
    Sorry buddy


    Thanks
    Matt

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    One thing I can think of is that you place the algorithm in a function and you have the value as a parameter and then if the first number isnt a prime call that function with n+1 as parameter. You then simply return the prime number.

    Oh and by the way, you REALLY only want to call sqrt once for every number to check, otherwise the algo will be slow.

    One last thing, you should get a habit of indenting your code, let me show you a small example:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    for(int i=0; i<10; i++)
    {
    cout << "This is unindented code and is hard to read" << endl;
    cout << "With more code it will be very hard to read if not indented" << endl;
    }
    cout << "So get the habit of indenting." << endl;
    }
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        for(int i=0; i<10; i++)
        {
            cout << "This is indented code and is easy to read" << endl;
            cout << "You will also catch errors such as missing a closing bracket more easily." << endl;
        }
        cout << "So get the habit of indenting." << endl;
    }
    See the difference
    Last edited by Shakti; 10-14-2004 at 02:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 05:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 11:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 04:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 05:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 04:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21