From d565bf851f96c40686bf1b6f2ea6ba2ef4796cb5 Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Thu, 21 Feb 2019 19:15:40 +0000 Subject: Thank stackOverflow --- ee1301/wk3/lab3/primeFactor.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'ee1301/wk3') 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 -#include #include 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); } -- cgit v1.2.3