This commit is contained in:
2019-12-04 00:34:49 +01:00
parent 755d074e47
commit 2483a50cef
4 changed files with 200 additions and 0 deletions
@@ -0,0 +1,26 @@
#include "cpp_headers/fcpp.hh"
int main() {
int n = enter_int("Please enter a natural number: ");
// search smallest factor, start with smallest possible: 2
int k=2;
// go until sqrt(n)
while (k <= sqrt(n)) {
printf("searching for dividers for %d, checking %d\n", n, k);
// k is factor of n
if (n % k == 0) {
// print out the factor k
print(k);
// reset n to the quotient
n = n / k;
printf("found %d, setting n=%d\n", k, n);
// restart at k=2
k = 2;
} else {
// if k is not a factor, check next one
k++;
}
}
// n is the last prime factor of the original input number
print(n);
}