C++ : Program that gives u the prime factors

Click For Summary

Discussion Overview

The discussion revolves around creating a C++ program that outputs the prime factors of a given number. Participants share code snippets, seek advice on implementation, and discuss optimization strategies. The focus is primarily on programming techniques and algorithm efficiency.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant shares an initial code snippet that displays all factors but struggles to isolate prime factors.
  • Another participant suggests a modified approach using a while loop to continuously divide the number by its factors until it cannot be divided further.
  • A participant questions the direction of the discussion, indicating uncertainty about how to proceed.
  • There is a request for advice on modifying the code, reflecting a lack of clarity on the next steps.
  • One participant acknowledges a mistake and mentions successfully implementing a while loop inside a for loop, indicating progress.
  • A later reply introduces the concept of optimizing the factor-checking process by suggesting that it is sufficient to check up to the square root of the number, along with examples to illustrate this point.
  • Additional efficiency tips are provided, such as skipping even numbers after checking for divisibility by 2 and multiples of other primes.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to implement the program, as multiple methods and optimizations are discussed without agreement on a single solution.

Contextual Notes

Some participants express uncertainty about specific programming techniques and optimization strategies, indicating a need for further clarification on the implementation details.

aleee
Messages
17
Reaction score
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()
{
count << "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;
}
count << i << " " << k << endl;
}
}
return 0;
}
 
Technology news on Phys.org
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:
any certain direction i should go in?
 
I don't understand your question.
 
im not sure on how to go about changing it. any advice?
 
Do you see the code in my post?
 
oh sorry my mistake. i ended up doing a while loop then a for loop inside, but thank you for the help
 
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.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
12
Views
3K
Replies
7
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K