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 27-Mar-2008, 17:27
Corrupt Shadow Corrupt Shadow is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Corrupt Shadow is on a distinguished road

Perfect Numbers


Don't you love classes that are required for your major that have nothing to do with what you'll be doing with your degree? Yay!

Okay, I have to write a complete program to write to the file perfect.out all perfect numbers between 2 and a specified named integer constant LIMIT (which we give a value of 1000).

This is what I have so far... but I have no clue how to use these functions in my main to get what I need to get. PLEASE HELP!!! I'm going crazy!

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

using namespace std;

int sumOfPositiveDivisors (int number)
{
 int sum = 0, denom = number, divisor = (number / denom);
    for (;;) {
        if ((number % denom) == 0) {
            sum += divisor;
        }
        denom--;
        if (denom == 1)
            return sum;   
}

bool isPerfect (int number)
{
     return number == sumOfPositiveDivisors (number);
}

int main()
{
    return 0;
{
  #2  
Old 27-Mar-2008, 19:38
C++_Bandit's Avatar
C++_Bandit C++_Bandit is offline
Junior Member
 
Join Date: Mar 2008
Location: Virginia
Posts: 40
C++_Bandit will become famous soon enough

Re: C++ Help -- Perfect Numbers


By perfect number, you mean the mathematical definition: a number whose divisors summed together equal that number. For example 6 whose divisors are 1, 2, and 3.
1+2+3 = 6
or 28
1+2+4+7+14 = 28
Right?
  #3  
Old 27-Mar-2008, 20:02
Corrupt Shadow Corrupt Shadow is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Corrupt Shadow is on a distinguished road

Re: C++ Help -- Perfect Numbers


Yes, that is correct.
  #4  
Old 28-Mar-2008, 01:59
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 57
mamntc02 has a spectacular aura about

Re: Perfect Numbers


An example of using your functions could be:
CPP / C++ / C Code:
int main(){
	int number = 20;
	cout << "Number  " << number << (isPerfect(number) ? " is perfect" : " is not perfect")<< endl;

	return 0;
}

However, your code won't compile, because in your sumOfPositiveDivisors you missed some closing braces.
Anyway, there's an easier way to whether a number is perfect. For example:
CPP / C++ / C Code:
bool isPerfect(int num)
{
    int sum = 0 ;
    int i;
 
    for( i = 1; i<num; i++ )
        if( num%i == 0 )
        	sum += i;
    if( sum == num )
        return true;
    else
        return false;
 
}

Well, it's not a optimized code, but works
Your code seems to be too much complicated, thought it has an only error, besides braces: Your divisor variable doesn't change with denom. It must stay in for loop.

Hope it helps!
Last edited by admin : 29-Mar-2008 at 05:00. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #5  
Old 29-Mar-2008, 08:13
Corrupt Shadow Corrupt Shadow is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Corrupt Shadow is on a distinguished road

Re: Perfect Numbers


I'd love to use that for isPerfect, but our professor specifically told us that isPerfect must call sumOfPositiveDivisors. Can I use that code in place of sumOfPositiveDivisors to the point where it looks like this?

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

using namespace std;

int sumOfPositiveDivisors (int number)
{
 int sum = 0 ;
    int i;
 
    for( i = 1; i<number; i++ )
        if( number%i == 0 )
        	sum += i;
    if( sum == number )
        return true;
    else
        return false;
}

bool isPerfect (int number);
{
     return number == sumOfPositiveDivisors (number);
}

int main();
{
    int number = 20;
	cout << "Number  " << number << (isPerfect(number) ? " is perfect" : " is not perfect")<< endl;

	return 0;
}
  #6  
Old 30-Mar-2008, 14:17
Corrupt Shadow Corrupt Shadow is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Corrupt Shadow is on a distinguished road

Re: Perfect Numbers


I updated my code a bit... I hate not knowing what the heck I'm doin. For an engineer-to-be, this is very irritating... HAHA!

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
#include <fstream.h>
using namespace std;

int sumOfPositiveDivisors (int number)
{
 int sum = 0 ;
    int i;
 
    for( i = 1; i<number; i++ )
        if( number%i == 0 )
        	sum += i;
    if( sum == number )
        return true;
    else
        return false;
}

bool isPerfect (int number);
{
     return number == sumOfPositiveDivisors (number);
}

int main();
{
    const int LIMIT = 10000
    int number;
	return isPerfect == 2 << LIMIT;
	ofstream outFile("perfect.cc",ios::app);
    outFile << "New line\n";
    outFile.close();
    return 0;
}
  #7  
Old 31-Mar-2008, 01:28
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 57
mamntc02 has a spectacular aura about

Re: Perfect Numbers


Hi,

I'm sure your program doesn't compile. Your must learn to read compiler error messages. For example, I've compiled your first code (without file-management) and the compiler prints:
Quote:
prova2.cpp:21: error: expected unqualified-id before '{' token
prova2.cpp:26: error: expected unqualified-id before '{' token
I admit is not a good message, but when you take a look at lines 21 and 26 you see:
CPP / C++ / C Code:
bool isPerfect (int number);
{
and
CPP / C++ / C Code:
int main();
{
You have an extra ; because it's a method definition.
Anyway, it won't compile, because you are comparing an integer (the number) with a bool value (true or false) in isPerfect. It makes no sense.

As I said in my previous message, your first code was good, though has a couple of errors: the braces you missed and not update divisor in each iteration. So, using your first code, this should work:
CPP / C++ / C Code:
int sumOfPositiveDivisors (int number)
{
	int sum = 0, denom = number, divisor = (number / denom);
	for (;;) {
		if ((number % denom) == 0) {
			divisor = (number / denom);
			sum += divisor;
		}
		denom--;
		if (denom == 1)
			return sum;   
	}
}

Regards.
  #8  
Old 31-Mar-2008, 05:37
Corrupt Shadow Corrupt Shadow is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Corrupt Shadow is on a distinguished road

Re: Perfect Numbers


CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
using namespace std;

int sumOfPositiveDivisors (int number)
{
	int sum = 0, denom = number, divisor = (number / denom);
	for (;;) {
		if ((number % denom) == 0) {
			divisor = (number / denom);
			sum += divisor;
		}
		denom--;
		if (denom == 1)
			return sum;   
	}
}

bool isPerfect (int number)
{
     return number == sumOfPositiveDivisors (number);
}

int main()
{
    const int LIMIT = 10000;
	return isPerfect == 2 << LIMIT;
	ofstream outFile("perfect.cc");
    outFile.close();
    return 0;
}

I still get errors in my MAIN:

Quote:
33 [Warning] left shift count >= width of type
34 variable `std::ofstream outFile' has initializer but incomplete type

What I need to do is print all perfect numbers (using isPerfect) from 2 to LIMIT (which we declare as 10,000) to the file "perfect.cc"
  #9  
Old 31-Mar-2008, 06:35
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 57
mamntc02 has a spectacular aura about

Re: Perfect Numbers


Of course, it won't compile!! What are you attempting to do with this?
CPP / C++ / C Code:
return isPerfect == 2 << LIMIT;
To print all perfect numbers up to a given number, you need a loop. For example:
CPP / C++ / C Code:
int main()
{
	const int LIMIT = 10000;

	ofstream outFile("perfect.cc", ios::app);
	
	for (int i = 2; i < LIMIT ; i++){
		if (isPerfect(i))
			outFile << i << endl;
	}

	outFile.close();
	return 0;
}
This is not the best loop possible, because perfect numbers are not odd, but I think it works

Regards
  #10  
Old 31-Mar-2008, 06:43
Corrupt Shadow Corrupt Shadow is offline
New Member
 
Join Date: Mar 2008
Posts: 8
Corrupt Shadow is on a distinguished road

Re: Perfect Numbers


Geez... I feel dumb now. ;)

I was thinking you could tell it to print all isPerfects from 2 to LIMIT in a return.

Duuurrrr...

The only problem Dev C++'s compiler is telling me is this:
35 variable `std::ofstream outFile' has initializer but incomplete type
 
 

Recent GIDBlogNot selected for officer school 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
recursive function for perfect numbers? cancan C Programming Language 6 19-Nov-2007 11:46
Prime or perfect numbers and output factors buomque C++ Forum 4 26-Oct-2007 09:23
subscript error in coding warborules C Programming Language 6 27-Nov-2005 17:16
Linear Search eccoflame C Programming Language 3 19-Apr-2005 08:36
Program that will find the first 4 perfect numbers????? bicknell83 C Programming Language 3 30-Mar-2005 01:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:13.


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