Finding prime factors of individual numbers

I'm trying to display the prime factors of each number between 1 and 100. I can find the prime factors of individual numbers, but when I try to do the same with each number between 1-100. I believe it's small, but I can't for the life of me find it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
cout << "Enter a number: ";
int num;
cin >> num;

cout<<"The prime factors are: ";



for (int i=2; i <= num; i++)
{
 while(num % i == 0)
 {
   num /= i;// "num" divided by "i" is now "num"
   answer+=i;
   cout<<i<<" ";
 }
}
}

The code above works perfectly and is for finding an individual number's prime factors. The code below is not working and just repeats "2" and should be for each number between 1 and 100.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main ()
{
    for(int test=2;test<100;test++)
    {
        for(int i=2;i<=test;i++)
        {
            while (test%i==0)
            {
                test /= i;
                cout<<i<<" ";
            }
        }
        cout<<endl;
    }
}


(I apologise for how unorganized my code is.)
Last edited on
In the second block of code.....

You are using 'test' as a loop counter on line 3.
You are then modifying 'test' on line 9.

What's happening is this:

1) outer for loop starts (line 3). test = 2
2) outer for loop condition is checked (line 3). It's true, so loop keeps going
3) inner for loop starts (line 5). i=2
4) inner for loop condition is checked (line 5). it's true, so the inner loop keeps going.
5) while loop condition is checked (line 7). It's true, so the while loop continues
6) test /= i (line 9). Since test and i are both 2, this means that now... test=1
7) print contents of i (line 10), which prints 2.
8) while loop condition is checked (line 7). It's false so the while loop exits.
9) inner for loop increment occurs (i++ on line 5). Now, i=3 and test=1
10) inner for loop condition checked (line 5). It's false, so the inner loop exits.
11) outer for loop increment occurs (test++ on line 3). Now, test=2
12) << Repeat from #4 >>


As you can see, since you keep dividing test by 2, test never gets any higher, and the endless stream of 2s are printed.

You might want to create two separate variables. One for the loop counter and one for the number whose factors you're searching for.
Last edited on
You start with
test=2
i=2

A factor, so you divide and end with
test=1
i=2

now i>test so the inner loops breaks.
the outer loop increase test, so now you have
test=2
i=2
Line 9 will keep modifying test, interfering with the for loop on line 3 that is trying to simply increment a number by one. You could use a temporary variable there instead.
Thank you very much, Disch, ne555, and Zhuge.

I added int vari=test after the first for loop, and replaced test in the nested loops with vari. It works perfectly now.

Once again, thank you.
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
42
43
44
45
46
47
48
49
50
51
/* PRIME FACTORIZATION PROGRAM *//* 60 = 2 * 2 * 3 * 5 */

#include <iostream>
#include <cmath>

main()
{
      int n,i,j,usernum;
      bool flag;
      j=0;
      
      std::cout<<"This program prime factorizes numbers."<<std::endl<<std::endl<<"Enter a number to be prime factorized."<<std::endl<<std::endl<<std::endl<<"  > ";
      std::cin>>usernum;
      
      int primes[usernum];
       
       for(n=2;n<usernum;n++)
       {
            flag=true;
            for(i=2;i<=sqrt(n);i++)
            if(n%i==0)
            {
                 flag=false;
                 break;
            }
            if(flag==true)
            {
                 primes[j]=n;
                 j++;
            }
       }
       
       std::cout<<std::endl<<std::endl;
       
       for(n=0;n<j;n++)
       {
            if(usernum%primes[n]==0)
            {
            	while(usernum%primes[n]==0)
            	{
            		std::cout<<" "<<primes[n]<<" ";
            		usernum=usernum/primes[n];
            	}
            }
       }
       
       if (usernum!=1)
       {
            std::cout<<" "<<usernum<<std::endl;
       }
}


DO NOT USE THIS CODE: 1. It takes up too much memory. 2. It is too expansive.
USE THIS CODE AS AN EXAMPLE.
Last edited on
Topic archived. No new replies allowed.