Created
July 28, 2010 16:27
-
-
Save deanmsands3/495131 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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