GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / 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 02-Apr-2008, 03:11
scrimshaw scrimshaw is offline
New Member
 
Join Date: Apr 2008
Posts: 2
scrimshaw is on a distinguished road

"Perfect" numbers - help with code/algorithm?


Hi everyone,

I'm trying to code a program that will calculate 'perfect numbers'. ( A perfect number is any positive integer that is equal to the sum of its divisors < n. )

The program is meant to scan an interval [a, b] and return the smallest number that is the closest to its sum of divisors.

e.g Whats its meant to look like:
Enter 2 positive integers a <= b: 2 28
The distance is minimal for 6 and is equal to 0

Here is the code...


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

int sum(int n );

int main()
{
   int a, b;
   cout << "Enter 2 positive integers a <= b: " << flush;
   cin >> a >> b;
   
   int c = b;   
   int d = a;
   int tmp;
   
   for( int n = a; n <= b; n++ )
   {
      tmp = n - sum(n);    
      if( tmp < 0 ) 
        tmp = -tmp;
   
          if( tmp <= c )
              tmp = c;
                d = n;  
   }
   
   cout << "The distance is minimal for "  << d << " and is equal to " << min << endl;
    
   system("pause");
   return 0;
}

// ----------------------------- //

int sum( int n )
{
   int s = 1;
   // if d is a divisor of n so is n/d avoid to search till n
   for( int d = 2; d*d <= n; d++ )
     if( n%d == 0 )
       s += d + n/d;    
   return s; 
}



Is there something wrong with my implementation? I have a feeling that my formulae function is incorrect, but I have no idea where to begin.
Any help is appreciated.
  #2  
Old 02-Apr-2008, 03:40
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 53
mamntc02 has a spectacular aura about

Re: "Perfect" numbers - help with code/algorithm?


Take a look a this post.

Regards.
__________________
Please, correct me if I'm wrong, and sorry for my english ;)
  #3  
Old 03-Apr-2008, 01:26
signorsinatra signorsinatra is offline
New Member
 
Join Date: Mar 2008
Posts: 5
signorsinatra is on a distinguished road

Re: "Perfect" numbers - help with code/algorithm?


Yes, but I believe that post refers to finding actual perfect numbers. This question is finding an ALMOST perfect number within a range of integers that has divisors that add up almost to the number itself and then also displaying the difference in the number and the sum of the divisors.

Personally I get completely lost with the use of d. I do not quite get why d is equal to a, and yet d is equal to 2 in the function sum.
  #4  
Old 03-Apr-2008, 06:27
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: "Perfect" numbers - help with code/algorithm?


Quote:
Originally Posted by signorsinatra
I have a feeling that my formulae function is incorrect, but I have no idea where to begin.
Any help is appreciated.

Quote:
Originally Posted by signorsinatra
Personally I get completely lost with the use of d. I do not quite get why d is equal to a, and yet d is equal to 2 in the function sum.
Where did your formula come from if you don't understand it? You need to sit down and work out a formula you understand before you begin coding. Look at the problem in steps

Quote:
Step 1) Get your range
Step 2) Work through your range number by number looking for a 'perfect number'
Step 2a) If you found a perfect number, celebrate and skip to step 4
Step 2b) If you didn't find a perfect number go to step 3
Step 3) Compare each number to the sum of it's divisors, keep only the one that is closes
Step 4) Print everything out

Now how many of the above steps have you done in your code? You've done step 1 and step 4 but I can't tell what the rest of your code is doing because you haven't commented anything nor do any of your variables have meaningful names.

CPP / C++ / C Code:
cout << "The distance is minimal for "  << d << " and is equal to " << min << endl;


Where the hell does 'min' come from? You've never declared it nor have you done anything to it. Your code doesn't even compile

CPP / C++ / C Code:
 for( int d = 2; d*d <= n; d++ )
     if( n%d == 0 )
       s += d + n/d; 
What does this do?

Have you traced this on paper? D will always be the biggest number input. You need to sit down and rework your formula.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #5  
Old 03-Apr-2008, 15:08
signorsinatra signorsinatra is offline
New Member
 
Join Date: Mar 2008
Posts: 5
signorsinatra is on a distinguished road

Re: "Perfect" numbers - help with code/algorithm?


Considering that I did not post this question. I don't understand why my name was put next to another's quotes. My post was commenting on my lack of understanding of another person's formula that I did not post.
  #6  
Old 03-Apr-2008, 17:10
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: "Perfect" numbers - help with code/algorithm?


Sorry, mistyped that. Those should all be attributed to scrimshaw
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

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
C code, matching binary pairs of numbers CeZ C Programming Language 4 13-Nov-2007 19:25
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
* ---- Fibonacci Numbers ---- * TimHDG C Programming Language 1 21-Nov-2004 13:34
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

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

All times are GMT -6. The time now is 04:22.


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