C++ : Program that gives u the prime factors

In summary, the program aims to display the prime factors of a given number. It uses a while loop and a for loop to check the integers up to sqrt(num) and find the prime factors. The code can be made more efficient by checking only certain integers, such as not checking even numbers after 2 or not checking multiples of 3 after 3.
  • #1
aleee
17
0
i need help making a program that only displays the prime factors
ex: 100 = 2*2*5*5

i got the program to display all the factors but after that I am lost.

here is the code so far
oh its in C++

#include <iostream>

using namespace std;

int main()
{
cout << "Enter a number: ";
int num;
cin >> num;
int i,k = 0;

for (int i=2; i <= num; i++)
{
if( num % i == 0)
{
if(num % i == 0)
{
k = num / i;
}
cout << i << " " << k << endl;
}
}
return 0;
}
 
Technology news on Phys.org
  • #2
Try to do this slightly differently:

Code:
#include <iostream>

using namespace std;

int main()
{
cout << "Enter a number: ";
int num;
cin >> num;

for (int i=2; i <= num; i++)
{
 while(num % i == 0)
 {
   num /= i;
   cout << i << " ";
 }
}
cout << endl;
return 0;
}
 
Last edited:
  • #3
any certain direction i should go in?
 
  • #4
I don't understand your question.
 
  • #5
im not sure on how to go about changing it. any advice?
 
  • #6
Do you see the code in my post?
 
  • #7
oh sorry my mistake. i ended up doing a while loop then a for loop inside, but thank you for the help
 
  • #8
Something to keep in mind is that you don't have to check all of the integers from 2 to num. It is sufficient to go up to sqrt(num) - or the largest integer greater than or equal to sqrt(num). The idea is that if num has a factor smaller than sqrt(num), then there will be one larger than sqrt(num) as well.

For example, suppose num is 85. All you need to do is check the integers from 2 though 9.

Check 2 - not a divisor.
Check 3 - not a divisor.
Check 4 - not a divisor.
Check 5 - is a divisor. (The other divisor is 17.)
Check 6 - not a divisor.
Check 7 - not a divisor.
Check 8 - not a divisor.
Check 9 - not a divisor.

FYI, you don't actually need to check 4, 6, 8, 10, etc. If 2 is not a divisor, then no other even integer will be a divisor. The same is true for 6, 9, 12, etc. If 3 isn't a divisor, then neither will any other multiple 3 be a divisor. There are lots of ways to make the program more efficient, but they add complexity to the code.
 

Related to C++ : Program that gives u the prime factors

1. What is a prime factor?

A prime factor is a number that is only divisible by 1 and itself. In other words, it is a number that cannot be broken down into smaller factors.

2. How do I find the prime factors of a number using C++?

To find the prime factors of a number using C++, you can use a loop to check for factors and a boolean function to determine if a number is prime. Then, you can print out the factors that are also prime.

3. Can a number have more than one set of prime factors?

Yes, a number can have multiple sets of prime factors. For example, the prime factors of 12 are 2 and 3, but it can also be expressed as 2, 2, and 3.

4. What is the significance of finding prime factors?

Finding prime factors is useful in many mathematical calculations, such as finding the greatest common divisor or simplifying fractions. It can also help in cryptography and number theory.

5. How can I optimize my program to find prime factors?

One way to optimize your program is to use a more efficient algorithm, such as the Sieve of Eratosthenes. Additionally, you can implement techniques like memoization or dynamic programming to avoid repeating calculations.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
22
Views
971
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
Back
Top