diff options
author | RossTheRoss <msattr@gmail.com> | 2019-02-21 19:15:40 +0000 |
---|---|---|
committer | RossTheRoss <msattr@gmail.com> | 2019-02-21 19:15:40 +0000 |
commit | d565bf851f96c40686bf1b6f2ea6ba2ef4796cb5 (patch) | |
tree | a0f296e12db6750e1c5e333edb36361a4dfd0b4f /ee1301/wk3/lab3 | |
parent | Fix things (diff) | |
download | homework-d565bf851f96c40686bf1b6f2ea6ba2ef4796cb5.tar homework-d565bf851f96c40686bf1b6f2ea6ba2ef4796cb5.tar.gz homework-d565bf851f96c40686bf1b6f2ea6ba2ef4796cb5.tar.bz2 homework-d565bf851f96c40686bf1b6f2ea6ba2ef4796cb5.tar.lz homework-d565bf851f96c40686bf1b6f2ea6ba2ef4796cb5.tar.xz homework-d565bf851f96c40686bf1b6f2ea6ba2ef4796cb5.tar.zst homework-d565bf851f96c40686bf1b6f2ea6ba2ef4796cb5.zip |
Thank stackOverflow
Diffstat (limited to '')
-rw-r--r-- | ee1301/wk3/lab3/primeFactor.cpp | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/ee1301/wk3/lab3/primeFactor.cpp b/ee1301/wk3/lab3/primeFactor.cpp index 1ec5bcc..0d0e8c1 100644 --- a/ee1301/wk3/lab3/primeFactor.cpp +++ b/ee1301/wk3/lab3/primeFactor.cpp @@ -1,21 +1,26 @@ #include <iostream> -#include <iomanip> #include <cmath> using namespace std; -int primefinder (long long n) { - for (long long i=n; i>1; --i) { - if (n%i==0 && n!=i) { - primefinder(i); - cout << i << "*"; - } +void factorFinder (unsigned long long n) { + unsigned long long i=2; + while (pow(i,2)<=n) { + if (n%i==0) { + cout << i << "*"; + n/=i; + } else { + i++; } - return composite; + } + if (n>1) { + cout << n << endl; + } } + int main () { - long long n; - cout << "Input a positive integer: "; + unsigned long long n; + cout << "Input a positive integer: "; cin >> n; - primefinder(n); - cout << endl; + cout << "Factors: "; + factorFinder(n); } |