Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Compiled under Debian GNU/Linux using g++
- Usage: PrimeFactor Number
- Released under the epic memes licence
- You should have recieved a copy with this, if not: 9gag.com/memes/licence.pptx
- */
- #include<iostream>
- #include<vector>
- #include<cstdlib>
- #include<unistd.h>
- using namespace std;
- bool IsPrime (int Number);
- vector <int> BubbleSortVector(vector <int> Vector);
- int main(int argc, const char* argv[])
- {
- if (argc != 2)
- {
- cout << "Usage: PrimeFactor Number" << endl;
- return 1;
- }
- cout << "Factoring " << atoi(argv[1]) << endl;
- cout.flush();
- vector <int> PrimeFactors;
- vector <int> CurrentFactors;
- vector <int> NextFactors;
- bool Done = false;
- bool loob = false; //lol
- int CheckTotal = 1;
- CurrentFactors.push_back(atoi(argv[1]));
- while (Done == false)
- {
- for (int i = 0; i < CurrentFactors.size(); i++)
- {
- for (int eye = 2; eye < CurrentFactors.at(i) && loob == false; eye++)
- {
- if (CurrentFactors.at(i) % eye == 0)
- {
- if (IsPrime(CurrentFactors.at(i) / eye))
- PrimeFactors.push_back(CurrentFactors.at(i) / eye);
- else NextFactors.push_back(CurrentFactors.at(i) / eye);
- if(IsPrime(eye)) PrimeFactors.push_back(eye);
- else NextFactors.push_back(eye);
- loob = true;
- }
- }
- loob = false;
- }
- CurrentFactors.erase(CurrentFactors.begin(), CurrentFactors.end());
- for (int i = 0; i < NextFactors.size(); i++)
- CurrentFactors.push_back(NextFactors.at(i));
- NextFactors.erase(NextFactors.begin(),NextFactors.end());
- CheckTotal = 1;
- for (int i = 0; i < PrimeFactors.size(); i++)
- CheckTotal = CheckTotal * PrimeFactors.at(i);
- if (CheckTotal == atoi(argv[1])) Done = true;
- }
- PrimeFactors = BubbleSortVector(PrimeFactors);
- cout << "The prime factors of " << atoi(argv[1]) << " are";
- for(int i = 0; i < PrimeFactors.size(); i++) cout << " " << PrimeFactors.at(i) << " ";
- cout << endl << endl;
- return 0;
- }
- bool IsPrime (int Number)
- {
- for(int i = 2; i < Number; i++) if(Number % i == 0) return false;
- return true;
- }
- vector <int> BubbleSortVector(vector <int> Vector)
- {
- bool Done = false;
- while (Done == false)
- {
- Done = true;
- for (int i = 0; i < Vector.size() - 1; i++)
- {
- if(Vector.at(i) < Vector.at(i+1))
- {
- int Temp = Vector.at(i+1);
- Vector.at(i+1) = Vector.at(i);
- Vector.at(i) = Temp;
- Done = false;
- }
- }
- }
- return Vector;
- }
- /*
- Output:
- abdul@debian:~/ProjectEuler$ ./PrimeFactor 8888
- Factoring 8888
- The prime factors of 8888 are 101 11 2 2 2
- */
Advertisement
Add Comment
Please, Sign In to add comment