sum of all factors taken from for loop c++


Is there a way to add all of these factors without using accumulate?

For some reason also now it's not listing 1 and the number any more, I changed value from 1 to 2, but I thought I changed it back after we figured out we needed 1 and the number.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You do not need to use anything you do not want to use.
Are you storing the factors, or did you want to accumulate them on the fly?
Do you want to use the one-loop modulo approach or do you want to continue with 2 loops?
In either approaches, how about setting total = 1 + N
and then skip this trivial case in your search for factors.

If two loops, I would go with x=2 .. N/2  and y = x..N/2 to speed things up. As you find two factors (being careful not to duplicate them (i.e., <a,b> is a duplicate of <b,a>), then you can just say:
    total += (x+y)
pacummingAuthor Commented:
>> If two loops, I would go with x=2 .. N/2  and y = x..N/2

can you elaborate on this?
Setting total initially to N+1 covers the case of the trivial factors of 1 and N.
So, by starting off with a candidate factor of 2, you are skipping the trivial case of 1.
Also, I noted once earlier that a factor of integer N cannot be greater than N/2, so no need to check beyond N/2. For example, If N=28, then a candidate factor of 15, 16, ... 27 cannot be since the smallest other possible factor is 2 (again, skipping the trivial case), and 15*2 = 30 > 28.

So, why have a loop going over candidate factors that you know cannot be factors.

As far as y = x..N/2, this enforces the idea of making sure that y is not less than x.
If N=28, then one pair of factors is <2,14>
So, if x=14, then in the y loop, we start off with y=14, and thus avoid the case where we have a pair of factors <14,2>. This not only eliminates duplicates, but makes the algorithm faster by eliminating cases that you do not care about.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

pacummingAuthor Commented:
if(factor == i && x > y){

I added that in there when you mentioned earlier and that seemed to take care of duplicates

So basically what I'm going to need to do to determine if the number is adundant for instance is figure out what the total of all factors added together would be and simply compare it to the number at hand.

Is there a way to keep up with every x and y that meets the conditions noted for each number individually?
>> simply compare it to the number at hand.
From your abundant link:
   "an abundant number or excessive number is a number n for which s(n) > 2n. Here s(n) is the sum-of-divisors function: the sum of all positive divisors of n, including n itself. The value s(n) - 2n is called the abundance of n."

So how you compare is dependant on what you are trying to report - "the abundance of n" or whether a number is "an abundant number or excessive number".

>> Is there a way to keep up with every x and y that meets the conditions noted for each number individually?
Sorry, I don't understand the question. Could you elaborate a little more?
pacummingAuthor Commented:
Sorry, every x and y that meets conditions meaning each x that turns out to be a factor is added to the sum and each y that is a factor is added as well. Basically for example if with current program if range 1 is selected (20-30) the program currently listed out 20 and all of it's factors 21 and all of it's factors etc.

I need to find out the sum of all the factors of each number using the code we currently have there.

x+y gives us for one set of factors but since x and y change each time the loop runs I don't know how to keep a running total of those to get my final resolution.
How about a concrete example of what you want. Suppose instead of a range of 11 numbers, we just have a range of 2 numbers - say, 20-21.
factors of 20: 1, 2, 4, 5, 10, 20
factors of 21: 1, 3, 7, 21

Now, please show explicitly what you would like your output to be if the range 20-21 were selected.
BTW - since I never heard of abundance before, could you clue me in on what this application is about? It's more interesting when clued in.
pacummingAuthor Commented:
sorry, here's a better explanation of what I'm doing

The proper divisors of an integer n are the positive integer divisors whose values are less than n. A positive integer is said to be a deficient, perfect, or abundant number if the sum of its proper divisors is, respectively, less than, equal to, or greater than the number. For example, 8 is deficient because its proper divisors are 1, 2, and 4, and 1 + 2 + 4 < 8; The integer 6 is perfect because its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6; The integer 12 is abundant because its proper divisors are 1, 2, 3, 4, and 6, and
1 + 2 + 3 + 4 + 6 > 12.

so the number itself is not included but 1 is.

factors of 20 = 1, 2 , 4, 5, 10, = 22 ... the program will know this but user will never see the factors or the total... output for ths would just have to be Abundant under classification header

factotrs of 21 :1, 3, 7 = 11 this will simply show Deficient


My output will list out

Number                   Classificiation
20                            Abundant
21                             Deficient
22                             etc
Now it's clear what you want.
For each number in the range, you set sumOfFactors =0;
>>     if(factor == i){
When this test is true, you have two factors, x and y. So, sumOfFactors += (x+y) in the body of this if statement.
When you have computed all the factors, sumOfFactors will be the sum of all factors. But you have to be careful about N being included in this sum. When you have the sum, then compare sum with N, and  print out the results accordingly. You can post your code in the "Code" box if you have problems.

Since the sum is not to include N, but it does include 1, I would set sumOfFactors =1 instead of 0. And then I would start the for loop with 2 instead of 1 since we have already included 1 in the sum.

For testing, I would make the range from 1 to 103 and see if you get "Abundant" for the following N:
   12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102, …  
(This is the answer from the link.)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pacummingAuthor Commented:
Thanks! I'll try this as soon as I get home later tonight, looks good though.

You were very helpful, I appreciate it!
Good luck! Post code if any problem. I'll be back tonight to check.
Can you tell me what is this notion of abundance used for and why are you interested in it?
pacummingAuthor Commented:
Online c++ beginner course so no professor in person... just a list of assignments. Trying to learn as much as I can from the book and ask questions that help me understand what's going on without asking too much. I feel okay about it now because I'm understanding the information you're giving me though.

Each assignment is forcing me to do a lot of research and learn a lot.

I'm definitely not trying to figure out if these numbers are abundant for fun though :)
Ah, and here I thought you were going for a PhD in Mathematical Number Theory.

Hey, if you're a beginner, you might consider learning this stuff yourself using books and the online resources, and after you have mastered it, then take the online course for a breeze. If you're going for a degree, then you can save a 1000's of $$'s by taking CLEP or DANTE exams. That's what my wife and two sons have done. Not too much in the computer area, but great in other areas.
Here are CLEP subjects in case you are interested:
pacummingAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.