![]() |
|
#1
|
|||
|
|||
C++ Perfect Number ProgramI need help!! I have a program to write the directions are" Write a function perfect that determines whether parameter number is a perfect number. Write a second function printFactor that prints the factors of a perfect number. Use the functions perfect and printFactor in a program that determines and prints all the perfect numbers between 1 and 1000 and the factors of each perfect number." I really need help!! If anyone can help me I hope it can happen by Monday morning!!!
|
|
#2
|
|||
|
|||
Re: C++ Perfect Number Program Help!!!!Quote:
CPP / C++ / C Code:
So the first thing is what will you need next. Maybe input of the number to be tested. CPP / C++ / C Code:
Then post back with specific question with what is giving you trouble. |
|
#3
|
|||
|
|||
Re: C++ Perfect Number Program Help!!!!The part was that is giving me that trouble is figuring out how to print the factors of the numbes and figuring out how to print out the perfect numbers from 1-1000! If you can help I would be really happy! this program is giving me the blues
|
|
#4
|
|||
|
|||
Re: C++ Perfect Number Program Help!!!!First of all, for the benefit of anyone who doesn't know what a perfect number is (including myself, until I looked this up):
Quote:
So, you need to Code:
Code:
|
|
#5
|
|||
|
|||
Re: C++ Perfect Number Program Help!!!!Quote:
I was with you up to here, and I like the explanation. However, the pseudo code is wrong. Quote:
It simply won't work for finding perfect numbers, since 1. If you start with i = 1 then the very first step incorrectly adds n to the list of factors, and you are dead from the start. 2. If you note that 1 is always a factor and, therefore you don't have to test for it, then you could add 1 to the list of factors before you do the loop, and start the loop with i = 2. This actually works for n = 6, the first perfect number. But sometimes that loop adds the same factor more than once. To illustrate, I'll put the '1' on a line by itself, then as I go through the loop for i = 2 up to and not including number/2, I put each pair of factors from your code on a separate line. For n = 28, a program based on your code would give something like the following: Code:
In fact, 28 is a perfect number since the sum of its (unique) factors is 1+2+4+7+14 = 28 So, pseudo code for adding the unique factors of n could be something like: Code:
Now if you wanted to print the factors (after determining that n is, indeed a perfect number), then you could go through the same kind of loop, but you would simply print the value of i whenever you found that (n % i) == 0. There may very well be more elegant ways (mathematically and programatically) to find perfect numbers, but I think it's most important to get the answers right at first. Then if insight gained by examining the method and the results of the simple-minded approach allows one to consider more efficient methods, I say, "Go for it!" Regards, Dave |
|
#6
|
|||
|
|||
Re: C++ Perfect Number Program Help!Hi,
I just recently came across a similar question, and here is the function I wrote to determine if a number is perfect or not: CPP / C++ / C Code:
Last edited by admin : 06-Oct-2008 at 22:34.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| Converting a number amount to text | Godzilla | C++ Forum | 5 | 31-Mar-2006 12:38 |
| Help with interactive program, please | nika1p2 | C Programming Language | 1 | 09-May-2005 01:22 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 21:25 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The