C - Find all amicable numbers between limits

半城伤御伤魂 提交于 2019-12-11 02:58:15

问题


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

回答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;
}



回答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) .

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



回答3:


Most of you might face problem understanding what amicable pairs are, let me explain it through an example 220 & 284 are said to be amicable pairs because if we find the proper divisors of 220 we get (1, 2, 4, 5, 10, 11, 20, 22, 44, 55 & 110) summing up all of them we get 284. Now, the proper divisors of 284 are (1, 2, 4, 71 & 142) summing up all of them we get 220. Similarly, the sum of the divisors of 1184 is equal 1210 & the sum of the divisors of 1210 is equal to 1184. Now, we write a program in C to find all the amicable pairs within the range of 10000.

int main()
    {
     int n,k;
     int i=1,s1=0,s2=0;

     for(k=1;k<=10000;k++)
        {
           n=k;
           while(i<n)
            {
             if(n%i==0)
             s1=s1+i;
             i++;
            }

          i=1;

          if(s1==n)
          continue;

          while(i<s1)
            {
             if(s1%i==0)
                s2=s2+i;
             i++;
            }

          if(n==s2)
          printf("%d \n",n);

          s1=0;
          s2=0;
       }
    }


来源:https://stackoverflow.com/questions/28267790/c-find-all-amicable-numbers-between-limits

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!