#include using namespace std; int number, loop_limit, results = 0; const int row_count = 8; // This version contains algorithm optimization that goes for the absolute // minimum possible number of loops to solve the same problem. // Note that any order to the printout of numbers must be ignored to do this. int main() { cout << "Enter a number: "; cin >> number; loop_limit = number; cout << number << " is divisible by:\n"; for(int i = 2; i < loop_limit; i++) { // Decrease loop_limit if new result is found if(number % i == 0) { cout << i << ' '; if(++results % row_count == 0) cout << endl; loop_limit = number/i; // Print two results per loop for max efficiency if(i != number/i) // Remove root duplicates { cout << number/i << ' '; if(++results % row_count == 0) cout << endl; } // Increment done twice to avoid row_count bugs } if(i % 2 != 0 && number % 2 != 0) // Rule exception i++; // Stops even numbers from checking on odd inputs } cout << endl; return 0; }