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

@phoffric

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.

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.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

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.

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.

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?

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?

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.

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.

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

etc.

My output will list out

Number Classificiation

20 Abundant

21 Deficient

22 etc

23

24

25

26

27

28

29

30

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 trialYou were very helpful, I appreciate it!

Can you tell me what is this notion of abundance used for and why are you interested in it?

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 :)

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.

http://www.collegeboard.com/student/testing/clep/exams.html

C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

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)