diff options
author | zedarider <ymherklotz@gmail.com> | 2016-02-26 21:15:09 +0000 |
---|---|---|
committer | zedarider <ymherklotz@gmail.com> | 2016-02-26 21:15:09 +0000 |
commit | f7a1296d353dea1f98b47d4baef87426ceadf6b5 (patch) | |
tree | c4fc565b3e2f672647033d639601f695782f1562 /C++/Factorisation.cpp | |
parent | c2d76b36f9f2ef70de599add53c08a38af3d1760 (diff) | |
download | imperial_2015-f7a1296d353dea1f98b47d4baef87426ceadf6b5.tar.gz imperial_2015-f7a1296d353dea1f98b47d4baef87426ceadf6b5.zip |
Organising into folders
Diffstat (limited to 'C++/Factorisation.cpp')
-rw-r--r-- | C++/Factorisation.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/C++/Factorisation.cpp b/C++/Factorisation.cpp new file mode 100644 index 0000000..034a975 --- /dev/null +++ b/C++/Factorisation.cpp @@ -0,0 +1,55 @@ +#include <iostream> +#include <cmath> +#include <vector> + +using namespace std; + +void is_prime(int); +void print_vector(vector<int>); + +void is_prime(int x) { + int original = x; + vector<int> primes, factors; + primes.push_back(2); + primes.push_back(3); + + for(int i = 5; i <= x; i++) { + bool itsPrime = false; + for(int j = 0; j < primes.size(); j++) { + if(i % primes[j] == 0) { + itsPrime = false; + break; + } else { + itsPrime = true; + } + } + if(itsPrime) { + primes.push_back(i); + } + } + // cout << "Prime numbers to consider: "; + // print_vector(primes); + while(x != 1) { + for(int l = 0; l < primes.size(); l++) { + if(x % primes[l] == 0) { + factors.push_back(primes[l]); + x = x / primes[l]; + break; + } + } + } + cout << "Factors of " << original << ": "; + print_vector(factors); +} + +void print_vector(vector<int> v) { + for(int k = 0; k < v.size(); k++) { + cout << v[k] << " "; + } + cout << endl; +} + +int main() { + is_prime(103); + return 0; +}
\ No newline at end of file |