Skip to content

Instantly share code, notes, and snippets.

@deanmsands3
Created July 28, 2010 16:27
Show Gist options
  • Save deanmsands3/495131 to your computer and use it in GitHub Desktop.
Save deanmsands3/495131 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
unsigned long X, sqrtX,iter;
vector<long> array;
cout<<"Enter Number you want factored: ";
cin>>X;
cout<<"The factors are as follows:"<<endl;
//Find the square root
sqrtX=(unsigned long)sqrt((double)X);
//Loop through finding factors up until iter==sqrtX
iter=1;
do{
if(X%iter==0){
array.push_back(iter);
}
iter++;
}while(iter!=sqrtX);
//Loop back through array starting at size-1,
//and divide into X, finding the rest of the factors.
iter=array.size();
do{iter--;
array.push_back(
X/array[iter]
);
}while(iter!=0); //I chose a do loop because I needed to break AFTER iter==0
//Print out all the factors
for(iter=0;iter<array.size();iter++)
cout<<array[iter]<<endl;
//For educational purposes
cout<<"Number of Factors: "<<array.size()<<endl;
cout<<"Integral Square Root:"<<sqrtX<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment