counting prime divisors

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cmath>
using namespace std;

void prime_divisor(int n){
	int d=2;
	while(d<=sqrt(n)){
		if(n%d==0){
			cout<<d<<"*";
			n=n/d;
			continue;
		}
		++d;
	}
	cout<<n<<endl;
}

int prime_decomposition_r(int n){
    int count=0;
	for(int d=1; d<=sqrt(n); ++d){
		if(n%d==0){
			count++;
			n=n/d;
			continue;
			count++;
		}
		count++;
	}
	return count;
	cout<<count<<endl;
}

int main(){
	int n=0;
	cout<<"n: ";
	cin>>n;
	cout<<"Type a positive integer:"<<n<<endl;
	cout<<n<<"=";
	prime_divisor(n);
	cout<<"There are "<<prime_decomposition_r(n)<<" prime factors.";
}
Last edited on
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.
I changed the code a bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cmath>
using namespace std;

void prime_divisor(int n){
	int d=2;
	while(d<=sqrt(n)){
		if(n%d==0){
			cout<<d<<"*";
			n=n/d;
			continue;
		}
		++d;
	}
	cout<<n<<endl;
}

int prime_decomposition_r(int n){
    int count=0;
	for(int d=2; d<=sqrt(n); ++d){

		if(n%d==0){
			++count;
		}
	}
	return count;
	cout<<count<<endl;
}

int main(){
	int n=0;
	cout<<"n: ";
	cin>>n;
	cout<<"Type a positive integer:"<<n<<endl;
	cout<<n<<"=";
	prime_divisor(n);
	cout<<"There are "<<prime_decomposition_r(n)<<" prime factors.";
}


It seems to work for some numbers so which means it's still wrong :(
Last edited on
¿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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

void primefact(unsigned long &n, unsigned long i, int &cnt) {
    for ( ; n % i == 0; n /= i) {
        if (cnt > 0) std::cout << " * ";
        std::cout << i;
        ++cnt;
    }
}

int primedecomp(unsigned long n) {
    int cnt = 0;
    primefact(n, 2, cnt);
    for (unsigned long 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(){
	unsigned long 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';
}

Last edited on
prime factorization has dupes.
8 is just 2*2*2
do you want an answer of 1 or 3 for 8?

its also skipping 2 (for 3...+=2) , so 8 would fail.

what do you expect to get when the value is prime? Eg, what do you think you would see for
137791 ?

Topic archived. No new replies allowed.