Trying to count prime divisors, tried doing it based on something i did before but it's wrong...... I just don't understand when for or while loop gets a little bit complicated, so can you please explain to me what i am doing wrong.
do a desk check.
`prime_divisor()' prints to the screen, it does not return a list of divisor or their number.
`prime_decomposition_r()' returns `n', that is, the input number. Also, it calls the other function several times with the same parameters.
¿so why you marked the thread as solved?
you've got a divisibility test in `prime_decomposition_r()' but you don't make sure to only test prime numbers or to test them all.
again, do a desk check.
#include <iostream>
void primefact(unsignedlong &n, unsignedlong i, int &cnt) {
for ( ; n % i == 0; n /= i) {
if (cnt > 0) std::cout << " * ";
std::cout << i;
++cnt;
}
}
int primedecomp(unsignedlong n) {
int cnt = 0;
primefact(n, 2, cnt);
for (unsignedlong i = 3; i * i <= n; i += 2)
if (n % i == 0) primefact(n, i, cnt);
if (n > 1) primefact(n, n, cnt);
std::cout << '\n';
return cnt;
}
int main(){
unsignedlong n = 0;
while (true) {
std::cout << "n: ";
if (!(std::cin >> n)) break;
if (n == 0) break;
int cnt = primedecomp(n);
std::cout << cnt << " prime factors\n";
}
std::cout << '\n';
}