Extracting Divisors: A Simple Guide for Adding Factors

  • Thread starter Thread starter soul5
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around a programming problem related to extracting divisors of a number and summing them. Participants are sharing code snippets and seeking assistance with the implementation in a programming context.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant seeks help on how to extract divisors of a number and sum them.
  • Another participant suggests initializing a sum variable and updating it within the if condition that checks for divisors.
  • A suggestion is made to use a vector to store individual divisors if needed.
  • One participant proposes optimizing the for loop to iterate only up to n/2, arguing that no divisors exist beyond that point.
  • A later reply points out that the for loop should include n itself as a divisor, recommending changing the loop condition to allow i to equal n.

Areas of Agreement / Disagreement

Participants generally agree on the need to include n as a divisor, but there are differing opinions on the efficiency of limiting the loop to n/2 versus including n in the loop.

Contextual Notes

Some code snippets contain minor syntax errors, such as missing semicolons, which may affect functionality. The discussion does not resolve these issues fully.

soul5
Messages
63
Reaction score
0
Ok this is what I did


int n;
count << enter number;
cin >> n;

for (int i = 1 ; i < n ; i++ )

if ( n % i == 0 )



I could use some help please like how to I extract the divisor?

so I can add them. Thanks a lot.
 
Last edited:
Technology news on Phys.org
Your if condition tells you if i is a divisor. Since you said you want to add up all the divisors you have to declare an identifier, say int sum=0; at the start.

So you can do this should the if statement be satisfied:

if(n%i==0)
sum += i;

If you're writing a function, then you'll want to return sum after you're done. Alternatively if you want all the divisors individually, one way is to initialise an integer vector, say vector<int> divisors (remember to #include <vector>), and use divisors.push_back(i); each time your if condition is true.

More importantly you can improve your program slightly by specifying your for loop as :
for(int i=1;i<=n/2;i++) since it is evident that there cannot be any divisors of n which is larger than n/2.
 
Last edited:
soul5 said:
int sum=0;
int n;
count << enter number;
cin >> n;

for (int i = 1 ; i <= n ; i++ ) {

if ( n % i == 0 )

sum += i;

}

count << " sum is " << sum << endl;

changed again
 
Last edited:
Well it looks ok to me. You forgot a semiconlon for your sum statement though. The best way to know if it'll work is to actually try it out. One thing overlooked earlier: your for loop doesn't allow i to be "n", which it should, since for any number n, n itself is a divisor of n. So change it to i<=n.
 

Similar threads

  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
7K
Replies
10
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K