Thread: Is it prime? Logic error

  1. #1
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92

    Is it prime? Logic error

    I've got a logic error in here and I am having trouble figuring it out.

    Can someone please point out the obvious as I cannot see it.

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main(){
    
        int i, n, squirt;
        bool is_prime = true;
    
        cout << "Enter a number: ";
        cin >> n;
    
        squirt = sqrt(static_cast<double>(n));
    
        for(i = 2; i <= squirt; i++){
            if(i % squirt == 0){
                is_prime = false;
                break;
            }
        }
    
        cout << n << " is ";
        if(!is_prime)
            cout << "not ";
        cout << "a prime number.";
    
        cout << endl;
    
        return 0;
    }
    TIA!
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Look carefully at line 16. It's completely out-to-lunch.

  3. #3
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by oogabooga View Post
    Look carefully at line 16.
    Thanks!

    Quote Originally Posted by oogabooga View Post
    It's completely out-to-lunch.
    Not sure what this means.

    Referring to previously working code, I fixed it with this:

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main(){
    
        int i, n, squirt;
        bool is_prime = true;
    
        cout << "Enter a number: ";
        cin >> n;
    
        squirt = sqrt(static_cast<double>(n));
    
        for(i = 2; i <= squirt; i++){
            if(n % squirt == 0){
                is_prime = false;
                break;
            }
        }
    
        cout << n << " is ";
        if(!is_prime)
            cout << "not ";
        cout << "a prime number.";
    
        cout << endl;
    
        return 0;
    }
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Line 16 is still wrong.

    out to lunch, Slang . not paying attention or tending to business; negligent: You must have been out to lunch when you wrote that weird report.

  5. #5
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by oogabooga View Post
    Line 16 is still wrong.
    Hard to break it, but I did.

    This doesn't seem to break:

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main(){
    
        int i, n, squirt;
        bool is_prime = true;
    
        cout << "Enter a number: ";
        cin >> n;
    
        squirt = sqrt(static_cast<double>(n));
    
        for(i = 2; i <= squirt; i++){
            if(n % i == 0){
                is_prime = false;
                break;
            }
        }
    
        cout << n << " is ";
        if(!is_prime)
            cout << "not ";
        cout << "a prime number.";
    
        cout << endl;
    
        return 0;
    }
    Thanks for your assistance.
    IDE + Complier: Code::Blocks w/ GCC
    PC: AMD Phenom x4 2.2Ghz w/ 8G RAM
    OSes: CentOS 6.2 x64 & Win 7 Ultimate x64

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible Logic Error
    By seanksg in forum C Programming
    Replies: 8
    Last Post: 03-14-2011, 06:42 PM
  2. logic Error
    By asmaa in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2009, 10:58 PM
  3. Some logic with prime-numbers
    By kenryuakuma in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2008, 09:38 AM
  4. Logic Error...
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 01-28-2006, 06:21 AM
  5. logic error I think
    By sscook69 in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2002, 10:08 PM

Tags for this Thread