?
Solved

sum of all factors taken from for loop c++

Posted on 2010-03-23
15
Medium Priority
?
574 Views
Last Modified: 2012-05-09
@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.
0
Comment
Question by:pacumming
  • 8
  • 7
15 Comments
 
LVL 33

Expert Comment

by:phoffric
ID: 28316865
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)
0
 

Author Comment

by:pacumming
ID: 28317337
>> If two loops, I would go with x=2 .. N/2  and y = x..N/2

can you elaborate on this?
0
 
LVL 33

Expert Comment

by:phoffric
ID: 28317701
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:pacumming
ID: 28318045
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?
0
 
LVL 33

Expert Comment

by:phoffric
ID: 28318456
>> 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?
0
 

Author Comment

by:pacumming
ID: 28342518
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.
0
 
LVL 33

Expert Comment

by:phoffric
ID: 28343457
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.
0
 

Author Comment

by:pacumming
ID: 28345096
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

etc.

My output will list out


Number                   Classificiation
20                            Abundant
21                             Deficient
22                             etc
23
24
25
26
27
28
29
30
0
 
LVL 33

Accepted Solution

by:
phoffric earned 2000 total points
ID: 28353604
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.)

0
 

Author Comment

by:pacumming
ID: 28360034
Thanks! I'll try this as soon as I get home later tonight, looks good though.

You were very helpful, I appreciate it!
0
 
LVL 33

Expert Comment

by:phoffric
ID: 28362003
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?
0
 

Author Comment

by:pacumming
ID: 28363889
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 :)
0
 
LVL 33

Expert Comment

by:phoffric
ID: 28364568
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.
0
 
LVL 33

Expert Comment

by:phoffric
ID: 28364776
Here are CLEP subjects in case you are interested:
    http://www.collegeboard.com/student/testing/clep/exams.html
0
 

Author Comment

by:pacumming
ID: 28365432
Thanks
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question