Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,337 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,114 people online right now. Registration is fast and FREE... Join Now!




C++ Programming

 
Reply to this topicStart new topic

C++ Programming

Vimrod
post 2 Apr, 2008 - 05:17 AM
Post #1


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 20

CODE

#include <iostream>
using namespace std;

void 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 - d;    
    if( tmp < 0 )
        tmp = -tmp;
      if( tmp <= c )
         tmp = c;
         d = c;  
   }
   cout << "The distance is minimal for "  << d << " and is equal to " << n << endl;
    
   system("pause");
   return 0;
}

void 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;
}

A perfect number n, is any positive integer that is equal to the sum of its divisors < n. For instance, 6 is a perfect number.
Indeed the divisors of 6 are 1, 2, 3 and 1 + 2 + 3 = 6. The next perfect number is 28. The next best thing is to have an integer n whose sum of divisors is as close as possible to n.

My program compiles and anything but just doesn't calculate right. Like I want it to scan an interval [a, b] and return the smallest number that is the closest to its sum of divisors. How do i do that?

My output is:
Enter 2 positive integers a <= b: 5 10
The distance is minimal for 10 and is equal to 11
Press any key to continue . . .

But the expected output is:
Enter 2 positive integers a <= b: 5 10
The distance is minimal for 6 and is equal to 0
Press any key to continue . . .

This post has been edited by Vimrod: 2 Apr, 2008 - 05:42 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 2 Apr, 2008 - 05:30 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,170



Thanked 33 times

Dream Kudos: 25
My Contributions


Can you provide an example of input you give the program, the output you receive, and what the expected output would be?
User is offlineProfile CardPM

Go to the top of the page

harshakirans
post 2 Apr, 2008 - 06:36 AM
Post #3


D.I.C Head

Group Icon
Joined: 26 Apr, 2006
Posts: 107



Dream Kudos: 150
My Contributions


Hi Vimrod,

Good atttempt so far, but you have to look at some things in your code

1. you have the defnition of the function sum which you havent invoked in your main(not calling anywhere in the program)

2. The expected output so called perfet number or a number near to it. your implemented logic doesnt contain the needed steps to find the perfect or nearly perfect number.

Here i post the algorithm to find the perfect number ( defined accorfing to you)

algorithm to find perfect no
1. for 1 to n/2
2. if n%i==0 then s+=i
3. if n-s<0 then diff[j]=s-n,num[j++]=n
else diff[j]=n-s,num[j++]=n

1. input a, b

2. for i between a and b
call perfect no passing i

3. now search for the least no in diff ..if diff[i] is least then num[i] is the answer so called perfect or near to perfect no.

Hope this helps.




Just check-out the following code if this solves your problem i hope it does.

CODE
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

int diff[10],num[10];
int j;

void calc(int n)
{
int i,s;
for(i=1;i<(n/2);i++)
{
if((n%i)==0)
s+=i;
if((n-s)<0){diff[j]=s-n;num[j++]=n;}else {diff[j]=n-s;num[j++]=n;}
}
}



void main()
{
int a,b;
printf("enter the min and max number");
cin>>a>>b;
for(int i=a;i<b;i++)
calc(i);
int temp=diff[0];
int no=0;
for(i=0;i<j;i++)
{
if(temp>diff[i+1])
{
temp=diff[i+1];
no=i+1;
}
}
printf("the number is %d",num[no]);
getch();
}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 03:28AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month