C - Find all amicable numbers between limits

13,682

Solution 1

You could write main() like this:

int main ()
{
    // assumes 1 <= min <= max                                                                
    long min = 1;
    long max = 10000;

    for (long a = min; a <= max; a++) {
        long b = sum_of_proper_divisors (a);
        if (a == b) {
            printf ("perfect number:\t%ld\n", a);
        }
        if ((a < b) && (b <= max) && (sum_of_proper_divisors (b) == a)) {
            printf ("amicable pair:\t(%ld, %ld)\n", a, b);
        }
    }

    return 0;
}

Solution 2

The most easiest and understandable way of finding amicable pairs between a range is as follows:

find amicable pairs between 1 to 2000.if you want between 1 to 3000 , just bring changes in the checking condition of for loops( i and j <= 3000). You can give whatever range you want (by changing the initialization and checking conditions of the loops(outer loop and inner loop) .

click here to look at the flow chart for better understanding

#include<stdio.h>

int main(){
    int i,j;
    //outer loop.
    for (i=1;i<=2000;i++){
        int d1=1;
        int sum1=0;
        while(d1<i){
            if(i%d1==0){
                sum1+=d1; //sum of divisors of i
                d1++;
            }else
                d1++;
        }
        //inner loop
        for(j=i+1;j<=2000;j++){
            int d2=1;
            int sum2=0;
            while(d2<j){
                if(j%d2==0){
                    sum2+=d2;//sum of divisors of j
                    d2++;
                }else
                    d2++;
            }

            if(sum1==j && sum2==i)
                //printing amicalbe pair.
                printf("(%d , %d) \n",i,j);
        }

    }

    return 0;
}
Share:
13,682

Related videos on Youtube

user3594736
Author by

user3594736

Updated on June 04, 2022

Comments

  • user3594736
    user3594736 almost 2 years

    First a definition: An amicable pair of numbers consists of two different integers where the sum of the divisors of the first integer is equal to the second integer, and the sum of the divisors of the second integer is equal to the first integer. A perfect number is a number that equals the sum of its own divisors.

    What I want to do is make a program that asks the user for a lower limit, and an upper limit and then presents him/her with all the amicable pairs (one per line) between those two limits. If there's a perfect number only one number needs to be printed (not a pair in its case).

    The whole idea is pretty confusing to me, so I'm looking for some assistance.

    Here's what I have to start with, I know that sumDivisors() should be more or less correct, but main() is merely checking if the two inputted numbers are amicable or not - might have to do a complete overhaul of this since I want all the pairs between two given limits.

    long sumDivisors(long a)
    {
        long s=0,i;
        for(i=1;i<=a/2;i++)
        {
            if(a%i==0)
            {
                s+=i;
            }
        }
        return s;
    }
    
    
    int main()
    {
        long t,n,s1;
        scanf("%ld",&t);
        while(t--)
        {
            scanf("%ld",&n);
            s1=sumDivisors(n);
            if(n==sumDivisors(s1))
            {
                printf("Yes\n");
            }
            else printf("No\n");
        }
        return 0;
    } 
    
    • user590028
      user590028 about 9 years
      I think anytime you get a homework assignment that involves math you not familiar with, you should check wikipedia. This is a great way to come up to quickly come up to speed with the concepts en.wikipedia.org/wiki/Amicable_numbers. Pay particular attention to the examples in the article which should help you with the range question you have.
    • user3594736
      user3594736 about 9 years
      That's actually where I got my definitions from, although still stuck on how to actually use it as a range and not use the two numbers to check.
  • user3594736
    user3594736 about 9 years
    I appreciate the help! I can see what happened now, and just fixed up my original code.