Home > Back-end >  Code optimization
Code optimization

Time:02-24

Required to achieve a simple calculation of integral factor and function, and another function, using the output of two positive integers m and n (0 & lt; M n 10000) or less or less between all the number, the number is the sum of the number in addition to its own equals the factor, for example: 6=1 + 2 + 3, among them 1, 2, 3 to 6 factors,

How to find a common factor simpler, cyclic code too many times to handle long
 # include & lt; stdio.h> 

Int factorsum (int number);
M void PrintPN (int, int n);

Int main ()
{
Int m, n;

The scanf (" % d % d ", & amp; M, & amp; N);
If (factorsum (m)==m) printf (" % d is a perfect number \ n ", m);
If (factorsum (n)==n) printf (" % d is a perfect number \ n ", n);
PrintPN (m, n);

return 0;
}

Int factorsum (int number)//determine whether to the number of
{
int i,j;
Int m=1;
Int n, num=0;
Int a [100]={0};
A [0]=1;
If (number==0)
return -1;
If (number==1)
return 1;
For (I=2; I & lt; Number; I + +)
{
For (j=I + 1; J & lt; Number; J + +)
{
If (I * j==number)
{
A [m]=I;
M + +;
A [m]=j;
M + +;
}

}
}
For (n=0; N & lt; 100; N + +)
{
If (a [n]==0)
break;
Num=num + a, [n].
}
If (num==number)
return num;
The else
return 0;

}

Void PrintPN (int m, int n)//find out between m and n number
{
Int q;
int i,j;
int num=0;

For (int k=m; k <=n; K++)
{
If (factorsum (k)==k)
{
Num=1;
Int p=1;
Int a [100]={0};
A [0]=1;
For (I=2; I & lt; k; I + +)
{
For (j=I + 1; J & lt; k; J + +)
{
If (I * j)==k
{
A [p]=I;
P + +;
A [p]=j;
P + +;

}
}
}
//order
Int b;
For (b=0; [b]!=0; B++)
{
For (int c=b + 1; A [c]!=0; C + +)
{
If (a [b] & gt; A [c])
{
Int the key;
Key=a [b];
A [b]=a [c];
A [c]=key;
}

}

}
Printf (" % d=", k);
For (q=0; Q & lt; 100; Q + +)
{
If (a [q]==0)
{
printf("\n");
break;
}
If (q==0)
Printf (" % d ", a [q]);
The else
Printf (" + % d ", a [q]);

}
}
}
If (num==0)
Printf (" No perfect number \ n ");

}


CodePudding user response:

Random search code of positive integer decomposition factor

CodePudding user response:

The original poster is too complex, for reference:
 # include & lt; stdio.h> 
Int factorsum (int n);//determine whether to the number of

Int main ()
{
Int m, n;
The scanf (" % d % d ", & amp; M, & amp; N);
If (m<=0 | | n> 10000) return 1;

For (int I=m; IIf (factorsum (I)) printf (" % d is a perfect number \ n ", I);
}

return 0;
}


Int factorsum (int n)
{
Int k=1, s=0;
While (k & lt;=n/2) {
If (n % k==0) s +=k;
k++;
}
If (s==n) return 1;
return 0;
}

CodePudding user response:

Fixed line 10 upstairs:
 for (int I=m; I<=n; I++) {

CodePudding user response:

 # include & lt; stdio.h> 

Int factorsum (int number);
M void PrintPN (int, int n);

The static void swap (int * a, int * b);
The static void print_perfect (int m, int n);

Int main ()
{
Int m, n;

The scanf (" % d % d ", & amp; M, & amp; N);

If (m & gt; N) {
Swap (& amp; M, & amp; N);
}

Print_perfect (m, n);
/*
If (factorsum (m)==m) printf (" % d is a perfect number \ n ", m);
If (factorsum (n)==n) printf (" % d is a perfect number \ n ", n);
PrintPN (m, n);
*/
return 0;
}

The static void swap (int * a, int * b)
{
Int TMP.

TMP=* a;
*=* b;
* b=TMP;
}

The static void print_perfect (int m, int n)
{
int i;

For (I=m; I & lt; n; I++) {
If (I==1)
continue;
{if (factorsum (I))
Printf (" % d is a perfect number! \ n ", I);
}
}
}


Int factorsum (int number)//determine whether to the number of
{
int i;
Int sum=1;


For (I=2; I & lt; Number/2 + 1; I++) {
If (number % I==0)
sum +=i;
}
If (sum==number)
return 1;

return 0;
/*
int i,j;
Int m=1;
Int n, num=0;
Int a [100]={0};
A [0]=1;
If (number==0)
return -1;
If (number==1)
return 1;
For (I=2; I & lt; Number; I + +)
{
For (j=I + 1; J & lt; Number; J + +)
{
If (I * j==number)
{
A [m]=I;
M + +;
A [m]=j;
M + +;
}

}
}
For (n=0; N & lt; 100; N + +)
{
If (a [n]==0)
break;
Num=num + a, [n].
}
If (num==number)
return num;
The else
return 0;
*/
}

Void PrintPN (int m, int n)//find out between m and n number
{
Int q;
int i,j;
int num=0;

For (int k=m; k <=n; K++)
{
If (factorsum (k)==k)
{
Num=1;
Int p=1;
Int a [100]={0};
A [0]=1;
For (I=2; I & lt; k; I + +)
{
For (j=I + 1; J & lt; k; J + +)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related