Judging Friendly Number Pairs (Function Topic)

topic description

Input two positive integers m and n, sequentially output all pairs of friendly numbers between m and n.

A logarithm is said to be friendly if the sum of all positive factors of two integers (including 1, excluding itself) is equal to the other. For example: 1184 and 1210 are friendly pairs, because the sum
of the factors of 1184 is 1+2+4+8+16+32+37+74+148+296+592=1210 The sum of the
factors of 1210 is 1+2+ 5+10+11+22+55+110+121+242+605=1184
requires the program to define a facsum () function and a main () function, the facsum () function calculates and returns the sum of all positive factors of n, and the rest The function is implemented in the main() function.
int facsum (int n)
{
// calculate the sum of all positive factors of n, this function returns sum
}

enter

Input two positive integers m and n, 1<m<=n<=10000.

output

Output all pairs of friendly numbers between m and n.
When outputting each friendly number pair, the decimal number is required to be in front of the large number, and the repeated number pairs are removed. For example, 220 and 284 are a pair of friendly numbers, and 284 and 220 are also a pair of friendly numbers. At this time, you only need to output the pair of friendly numbers 220 and 284. Each pair of friendly numbers occupies one line, and the two numbers are separated by spaces.

If there is no friendly pair between m and n, output "No answer".

sample input

100 8000

sample output

220 284
1184 1210
2620 2924
5020 5564
6232 6368

Tip: Do not output directly, otherwise the output will exceed the limit, save all the friendly real number pairs

#include <stdio.h>
#include <stdlib.h>
int fac( int n)
  {
      int i,m,sum=1;
         for (i=2;i<n;i++)
         {
         if (n%i==0)
             {
              m=n/i;
             sum=sum+i;//找N的因数并加起来;
             }
         }
         return sum;
}
int main( )
  {
      int m,n,i,j,flag=0;
          scanf ( "%d%d" ,&m,&n);
           int s[n];
                for (i=0;i<=n-m;i++)
                     s[i]=fac(i+m);//将每个数的因数和都保存下来。
                for (i=m;i<n;i++)
                     {
                         j=s[i-m];
                if (j>i&&j<=n&&s[j-m]==i)
                     flag=1,//判断是否有值。
                     printf ( "%d %d\n" ,i,j);
                }
                if (!flag)
                 printf ( "No answer\n" );
  
return 0;

Guess you like

Origin blog.csdn.net/zs520ct/article/details/42871603