aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk3
diff options
context:
space:
mode:
authorRossTheRoss <msattr@gmail.com>2019-02-21 19:15:40 +0000
committerRossTheRoss <msattr@gmail.com>2019-02-21 19:15:40 +0000
commitd565bf851f96c40686bf1b6f2ea6ba2ef4796cb5 (patch)
treea0f296e12db6750e1c5e333edb36361a4dfd0b4f /ee1301/wk3
parentFix things (diff)
downloadhomework-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 'ee1301/wk3')
-rw-r--r--ee1301/wk3/lab3/primeFactor.cpp29
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);
}