As an example of the modifications I mentioned above, here is the same thing Nebulus200 did, but with some minor modifications that should result in a net speed gain.
Just a simple rework of the code originally posted by nebulus200.Code:#include <iostream.h> int main(int argc, char **argv) { int number = 0; int altnum = 0; int total = 0; cout << "Please enter the number you wish to check for factors.\n"; cin >> number; for (int i = 2; i < (number / 2); i++) { altnum = number / i; if (number % i == 0) { cout << "Number " << i << " is a factor of " << number << ".\n"; cout << "Number " << altnum << " is a factor of " << number << "\n"; total += 2; } } if (! total) { cout << "Number " << number << " appears to be prime.\n"; } else { cout << "Found " << total << " factors.\n"; } return 0; }




Reply With Quote