| CODE |
| // Tubapro's prime tester with a main() #include <iostream> #include <ctype> #include <cmath> using namespace std; int IntPrime(int a) { if(a < 0) a /= -1; int con; int i = 3; if(a == 1) return (1); if((a == 2)||(a == 3)) return (0); else { const double root = sqrt( (double)a ) + 1; con = a % 2; if(con == 0) { return (2); } else { while(i < root) { con = a % i; if(con == 0) return (i); i += 2; // no need to try dividing by evens if not divisble by 2 } } } return (0); } int main() { int input; int out; cout << "Type a number (you must type a digit): "; cin >> input; cout << "\n\n"; out = IntPrime(input); if(out == 0) { cout << input << " is a prime number." << endl; } else if (out == 1) { cout << "One is never a prime number." << endl; } else { cout << input << " is divisible by " << out << "." << endl; } cin.sync(); cin.get(); return 0; } |
| CODE |
| int IntPrime(int a) { // Very interesting way of doing abs. // Note that the '/' operator is probably the slowest mathimatical operation // Why not just use a = 0-a or a = abs(a) or a = a*-1 if(a < 0) a /= -1; // Dont decalre variables before ou need them // When i got to the while loop it took me a while to find the declaration of 'i' int con; int i = 3; // What about if a == 0? if(a == 1) return (1); if((a == 2)||(a == 3)) return (0); // Dont need this else // The previous two if statment have already returned. else { // Dont use C cast. // Use the C++ static_cast<double>() // In this case the cast is not event required. const double root = sqrt( (double)a ) + 1; con = a % 2; if(con == 0) { return (2); } // Dont need else because of return else { // The compiler will have to covert i to a double before each comparison. // Why not make root an integer to avoid this problem. while(i < root) { con = a % i; if(con == 0) return (i); i += 2; // no need to try dividing by evens if not divisble by 2 } } } return (0); } |
| CODE |
int IntPrime(int a) { a = abs(a); if (a == 0) {return(XXX);} // Not sure what to return. if (a == 1) {return (1);} if((a == 2) || (a == 3)) {return (0);} if ((a %2) == 0) {return (2);} const int root = static_cast<int>(sqrt(a) + 1); for(int i = 3;i < root;i+=2)// no need to try dividing by evens if not divisble by 2 { if ((a % i) == 0) { return (i); } } return (0); } |
| QUOTE (C-Man @ Mar 12 2005, 09:25 AM) |
| because it's cctype/ctype.h not ctype and btw you can remove that include cause none of the functions are used |
| CODE |
| #include <iostream> #include <cmath> using namespace std; unsigned long LongPrime(unsigned long a) { if (a == 0) { return -1; } else if (a == 1) { return 1; } else if((a == 2) || (a == 3)) { return 0; } if ((a % 2) == 0) { return 2; } const long root = static_cast<unsigned long>(sqrt((static_cast<double>(a)) + 1)); for(int i = 3; i < root; i += 2) { if ((a % i) == 0) { return i; } } return 0; } int main() { bool run = true; while(run) { unsigned long input; short out; cout << "Type a number to test if it is a prime \nnumber [you must type digit(s), 0 to quit]: "; cin >> input; if(input == 0) { run = false; break; } cout << "\n\n"; out = LongPrime(input); switch(out) { case 0: cout << input << " is a prime number." << endl; break; case 1: cout << "One is never a prime number." << endl; break; case 2: cout << "Even numbers are never prime, except two." << endl; break; default: cout << input << " is divisible by " << out << "." << endl; } } cin.sync(); cin.get(); return 0; } |