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 04-Apr-2008, 03:41
New_Girl991 New_Girl991 is offline
New Member
 
Join Date: Apr 2008
Posts: 4
New_Girl991 is on a distinguished road

Help! Need Formula For Perfect Number


HELP. I have to create a formula that scans an interval [a, b] and return the smallest number that is the closest to its sum of divisors for a perfect number.


Since perfect numbers are very rare, it is possible that there is no perfect number in a given interval.
Instead we would like to find a number which is as close as possible to the
sum of its divisors. So the program will go through a certain interval (provided by the user) and return the
integer in this interval which minimizes the distance to its divisors.

For instance if you enter 10 and 16, the program should compute the sum of divisors for each n in [10, 16]

The divisors of 10 are 1,2,5 and the sum is 8 so the distance between 10 and 8 is 10 - 8 = 2
The divisor of 11 is 1 and the sum is 1 so the distance is 11 - 1 = 10
The divisors of 12 are 1,2,3,4,6 and the sum is 16 so the distance is 16 - 12 = 4
The divisor of 13 is 1 and the sum is 1 so the distance is 13 - 1 = 12
The divisors of 14 are 1,2,7 and the sum is 10 so the distance is 14 - 10 = 4
The divisors of 15 are 1,3,5 and the sum is 9 so the distance is 15 - 9 = 6
The divisors of 16 are 1,2,4,8 and the sum is 15 so the distance is 16 - 15 = 1

And then decide that the integer n whose sum of divisors is the closest to n is 16. Indeed the distance to the
sum of its divisors is just 1, which is minimal across all the distances.
  #2  
Old 04-Apr-2008, 04:30
scrimshaw scrimshaw is offline
New Member
 
Join Date: Apr 2008
Posts: 2
scrimshaw is on a distinguished road

Re: Help! Need Formula For Perfect Number


I guess you're going to have to calculate all the divisors for a number with a test that checks to see if a potential divisor has a remainder.
If there is a remainder, you have to kick it out and go to the next potential divisor.
Sum the divisors as you go along and then once you have them all.. test to see if they are equal to the potential perfect number.
  #3  
Old 04-Apr-2008, 04:36
New_Girl991 New_Girl991 is offline
New Member
 
Join Date: Apr 2008
Posts: 4
New_Girl991 is on a distinguished road

Re: Help! Need Formula For Perfect Number


Thanks scrimshaw.. I am only new to this.. I know this is what I have to do..
But that is the formula that I am stuck on. How do I do that?
  #4  
Old 04-Apr-2008, 04:37
New_Girl991 New_Girl991 is offline
New Member
 
Join Date: Apr 2008
Posts: 4
New_Girl991 is on a distinguished road

Re: Help! Need Formula For Perfect Number


I can work it out in my head, but not put it into the program.. I have only been doing this for a month now. I am an amatuer but I want to learn
  #5  
Old 04-Apr-2008, 04:48
New_Girl991 New_Girl991 is offline
New Member
 
Join Date: Apr 2008
Posts: 4
New_Girl991 is on a distinguished road

Re: Help! Need Formula For Perfect Number


BTW. This is my code.
I think I have almost done it...??

CPP / C++ / C Code:
#include <iostream>
using namespace std;
 
 
int sumTotal( int n ) // this function is to work out the sum of the divisors
{
int d = 0;
int i = 1;
 
for( i = 1; i <= n/2; i++ )
{
if ( n%i == 0 ); 
cout << n << endl;
}
return d; 
}
 
 
int main()
{
int a, b, temp2, min;
cout << "Enter 2 positive integers a <= b: " << flush;
cin >> a >> b;
 
int c = b; 
int d = a;
temp2 = 0;
 
//cout << "this is my function " << flush;
//cout << sumTotal << endl;
 
for( int n = a; n <= b; n++ )
{
temp2 = n - sumTotal ( n ); 
if( c < 0 ) 
{
temp2 = n - sumTotal ( n );
}
if( temp2 < c )
{
c = temp2;
}
cout << "n " << n << "b " << b << "c " << c << endl;
// cout << sum(n) << endl;
 
} 
 
for ( int n = a; n <= b; n++ )
{
min = n - sumTotal(n);
//cout << "this is min " << min << "n " << n << flush;
 
sumTotal(n);
if (min == 0)
// cout << "min " << min << endl;
{
min = 0;
cout << "The distance is minimal for " << n << " and is equal to " << min << endl;
system ("pause");
return 0; 
}
if (c <= min)
{
cout << "The distance is minimal for " << n << " and is equal to " << min << endl;
system ("pause");
return 0; 
}
}
cout << "The distance is minimal for " << d << " and is equal to " << min << endl;
 
 
system("pause"); 
return 0;
Last edited by admin II : 05-Apr-2008 at 04:03. Reason: Please surround your C++ code with [cpp] your code [/cpp]
 
 

Recent GIDBlogNARMY 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
Knights Tour - Reloaded . kobi_hikri C Programming Language 12 03-Oct-2005 12:15
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Array..... chriskan76 C Programming Language 4 18-Oct-2004 14:19
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19

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

All times are GMT -6. The time now is 23:32.


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