Skip to content

Instantly share code, notes, and snippets.

@tyler-boyd
Last active June 9, 2016 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tyler-boyd/10800371 to your computer and use it in GitHub Desktop.
Save tyler-boyd/10800371 to your computer and use it in GitHub Desktop.
Quickfactor.cpp
#include <vector>
#include <iostream>
using namespace std;
vector<int> factorize(int num){
int smallest_pairing = num;
vector<int> factors;
for(int i=1; i<smallest_pairing; i++)
{
int pairing = num/i;
if(float(num)/float(i) == pairing)
{
factors.push_back(i);
factors.push_back(pairing);
smallest_pairing = pairing;
}
}
return factors;
}
int main() {
int num = -1;
cout << "Number? ";
cin >> num;
cout << endl << endl;
if(num < 0)
{
cout << "Bad" << endl;
return -1;
}
vector<int> factors = factorize(num);
for(int i=0; i<factors.size(); i++)
{
cout << "Factor: " << factors.at(i) << endl;
}
cout << "Done!" << endl << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment