Files
uni/ws2019/ipi/uebungen/primfaktorzerlegung.cpp
T
2019-12-04 00:35:59 +01:00

25 lines
636 B
C++

#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)) {
// k is factor of n
if (n % k == 0) {
// print out the factor k
print(k);
// reset n to the quotient
n = n / k;
// 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);
}