GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Oct-2006, 02:33
lpwilliams21 lpwilliams21 is offline
New Member
 
Join Date: Oct 2006
Posts: 2
lpwilliams21 is on a distinguished road
Smile

C++ Perfect Number Program


I 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  
Old 08-Oct-2006, 09:52
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: C++ Perfect Number Program Help!!!!


Quote:
Originally Posted by lpwilliams21
I really need help!! If anyone can help me
I would say just pick one thing at a time to accomplish. The first thing you will need is a basic skeleton.

CPP / C++ / C Code:
#include <iostream>
#include <ostream>

using namespace std;

int main()
{

	return 0;
}
Now add one thing at a time. Getting what you have added working 100%. Then move onto the next thing.

So the first thing is what will you need next. Maybe input of the number to be tested.

CPP / C++ / C Code:
#include <iostream>
#include <ostream>

using namespace std;

int main()
{
	int var1 =0;
	cout << "Enter number : ";
	cin >> var1;

	return 0;
}
and so on ...

Then post back with specific question with what is giving you trouble.
  #3  
Old 08-Oct-2006, 12:02
lpwilliams21 lpwilliams21 is offline
New Member
 
Join Date: Oct 2006
Posts: 2
lpwilliams21 is on a distinguished road

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  
Old 08-Oct-2006, 13:47
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

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:
Originally Posted by Wikipedia
In mathematics, a perfect number is defined as an integer which is the sum of its proper positive divisors, that is, the sum of the positive divisors not including the number.

So, you need to
Code:
loop through i=1 to 1000 find factors of i (including 1, but not including i) add up said factors if the sum == i, print out i and the set of factors end loop
I gather you need some help with finding the factors of a number. The key is using the modulus operator to determine whether a certain number is divisible by another one. The modulus operator does division, and returns the remainder. For example, 5 % 4 = 1, but 8 % 2 = 0. If the remainder is 0, the numbers divide evenly.
Code:
loop through i=1 to number/2 if number % i == 0 add i and number/i to the list of factors end loop
  #5  
Old 08-Oct-2006, 16:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ Perfect Number Program Help!!!!


Quote:
Originally Posted by ubergeek
The modulus operator does division, and returns the remainder. For example, 5 % 4 = 1, but 8 % 2 = 0. If the remainder is 0, the numbers divide evenly.

I was with you up to here, and I like the explanation. However, the pseudo code is wrong.
Quote:
Originally Posted by ubergeek
Code:
loop through i=1 to number/2 if number % i == 0 add i and number/i to the list of factors end loop

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:
The factors of 28: 1 2 14 4 7 7 4 sum = 39

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:
sum = 0 loop through i=1 up to but not including n if n % i == 0 sum += i end if end loop

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  
Old 06-Oct-2008, 21:49
yohosuff yohosuff is offline
New Member
 
Join Date: Oct 2008
Posts: 1
yohosuff is on a distinguished road

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:
bool perfect(int number)
{
    int sum = 0;

    for(int i = 1; i <= number/2; ++i)
    {
        if(number%i == 0)
        {
            sum+=i;
        }

    }


    if(sum==number)
        return 1;
    else
        return 0;
Last edited by admin : 06-Oct-2008 at 22:34. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 16:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.