-1

I have a method named henry that takes two integer arguments, i and j, and returns the sum of the ith and jth perfect numbers. For example, henry(1, 3) should return 502 because 6 is the 1st perfect number and 496 is the 3rd perfect number and 6 + 496 = 502.

int henry (int i, int j)
 {
      //how do i start 
 }

I know how to print perfect number like this:

int main()  
{  
    int i, j, n, sum = 0;  

    /* Reads upper limit to print perfect numbers upto */  
    printf("Enter any number to print perfect number up to: ");  
    scanf("%d", &n);  



    printf("\nAll Perfect numbers between 1 to %d:\n", n);  


    /* 
     * Iterates from 1 to n and print if it is perfect number 
     */  
    for(i=1; i<=n; i++)  
    {  
        sum = 0;  

        /* 
         * Checks whether the current number i is Perfect number or not 
         */  
        for(j=1; j<i; j++)  
        {  
            if(i%j==0)  
            {  
                sum += j;  
            }  
        }  

        /* If the current number i is Perfect number */  
        if(sum == i)  
        {  
            printf("%d is Perfect Number\n", i);  
        }  
    }  

    return 0;  
}  

Please give me hint to solve this problem, thank you.

T0xicCode
  • 4,583
  • 2
  • 37
  • 50
  • The algorithm you have used here is very slow, and I think you won't find more than 4 (6, 28, 496, 8128) – 0xEDD1E Apr 05 '16 at 16:46
  • Start by changing the `i` and `j` arguments for `henry` to something else, such as `a` and `b` that do not conflict with the `i` and `j` used in your perfect sum testing that will have to implemented in `henry`. It might then be clearer how to solve it. – Weather Vane Apr 05 '16 at 16:56

1 Answers1

1
int henry(int i,int j)
{
int count=0,k=1,s=0;
  while(count<=i || count<=j)
  {
   int sum=0;
   for(p=1; p<=k/2; p++)  
    {  
        if(k%p==0)  
        {  
            sum += p;  
        }  
    }
   if(sum==k)
   count++;
   if(count==i||count==j)
   s+=k;
   k++;
  }
if(i!=j)
return s;
else
return 2*s;
}
Shubham Agrawal
  • 559
  • 6
  • 15